langtools/src/jdk.jshell/share/classes/jdk/jshell/Wrap.java
changeset 37644 33cf53901cac
parent 36780 6bf2bef08a91
child 38535 4a25025e0b0d
equal deleted inserted replaced
37643:626e07816dce 37644:33cf53901cac
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package jdk.jshell;
    26 package jdk.jshell;
    27 
    27 
       
    28 import java.util.Arrays;
       
    29 import static java.util.stream.Collectors.joining;
    28 import static jdk.internal.jshell.remote.RemoteCodes.DOIT_METHOD_NAME;
    30 import static jdk.internal.jshell.remote.RemoteCodes.DOIT_METHOD_NAME;
    29 
    31 
    30 /**
    32 /**
    31  * Wrapping of source into Java methods, fields, etc.  All but outer layer
    33  * Wrapping of source into Java methods, fields, etc.  All but outer layer
    32  * wrapping with imports and class.
    34  * wrapping with imports and class.
   235                 }
   237                 }
   236             }
   238             }
   237             return 0;
   239             return 0;
   238         }
   240         }
   239 
   241 
       
   242         Wrap wrapIndexToWrap(long wi) {
       
   243             int before = 0;
       
   244             Wrap w = null;
       
   245             for (Object o : os) {
       
   246                 if (o instanceof String) {
       
   247                     String s = (String) o;
       
   248                     before += s.length();
       
   249                 } else if (o instanceof Wrap) {
       
   250                     w = (Wrap) o;
       
   251                     int len = w.wrapped().length();
       
   252                     if ((wi - before) <= len) {
       
   253                         //System.err.printf("Defer to wrap %s - wi: %d. before; %d   -- %s  >>> %s\n",
       
   254                         //        w, wi, before, w.debugPos(wi - before), w.wrapped());
       
   255                         return w;
       
   256                     }
       
   257                     before += len;
       
   258                 }
       
   259             }
       
   260             return w;
       
   261         }
       
   262 
   240         @Override
   263         @Override
   241         public int wrapIndexToSnippetIndex(int wi) {
   264         public int wrapIndexToSnippetIndex(int wi) {
   242             int before = 0;
   265             int before = 0;
   243             for (Object o : os) {
   266             for (Object o : os) {
   244                 if (o instanceof String) {
   267                 if (o instanceof String) {
   284                 }
   307                 }
   285             }
   308             }
   286             return 0;
   309             return 0;
   287         }
   310         }
   288 
   311 
       
   312         Wrap wrapLineToWrap(int wline) {
       
   313             int before = 0;
       
   314             Wrap w = null;
       
   315             for (Object o : os) {
       
   316                 if (o instanceof String) {
       
   317                     String s = (String) o;
       
   318                     before += countLines(s);
       
   319                 } else if (o instanceof Wrap) {
       
   320                     w = (Wrap) o;
       
   321                     int lns = countLines(w.wrapped());
       
   322                     if ((wline - before) < lns) {
       
   323                         return w;
       
   324                     }
       
   325                     before += lns;
       
   326                 }
       
   327             }
       
   328             return w;
       
   329         }
       
   330 
   289         @Override
   331         @Override
   290         public int wrapLineToSnippetLine(int wline) {
   332         public int wrapLineToSnippetLine(int wline) {
   291             int before = 0;
   333             int before = 0;
   292             for (Object o : os) {
   334             for (Object o : os) {
   293                 if (o instanceof String) {
   335                 if (o instanceof String) {
   313         @Override
   355         @Override
   314         public int lastSnippetLine() {
   356         public int lastSnippetLine() {
   315             return snlineLast;
   357             return snlineLast;
   316         }
   358         }
   317 
   359 
   318 
   360         @Override
       
   361         public String toString() {
       
   362             return "CompoundWrap(" + Arrays.stream(os).map(u -> u.toString()).collect(joining(",")) + ")";
       
   363         }
   319     }
   364     }
   320 
   365 
   321     private static class RangeWrap extends Wrap {
   366     private static class RangeWrap extends Wrap {
   322 
   367 
   323         final Range range;
   368         final Range range;
   402         @Override
   447         @Override
   403         public int lastSnippetLine() {
   448         public int lastSnippetLine() {
   404             return lastSnline;
   449             return lastSnline;
   405         }
   450         }
   406 
   451 
       
   452         @Override
       
   453         public String toString() {
       
   454             return "RangeWrap(" + range + ")";
       
   455         }
   407     }
   456     }
   408 
   457 
   409     private static class NoWrap extends RangeWrap {
   458     private static class NoWrap extends RangeWrap {
   410 
   459 
   411         NoWrap(String unit) {
   460         NoWrap(String unit) {