]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/parsing/declarations/DDocumentationAst.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / parsing / declarations / DDocumentationAst.java
1 package org.simantics.scl.compiler.internal.parsing.declarations;
2
3
4 public class DDocumentationAst extends DeclarationAst {
5     public final String documentation;
6     
7     public DDocumentationAst(String documentation) {
8         this.documentation = cleanUp(documentation);
9     }
10
11     @Override
12     public void toString(int indentation, StringBuilder b) {
13         for(int i=0;i<indentation;++i) b.append("    ");
14         b.append("\"\"\"\n");
15         b.append(documentation);
16         b.append("\"\"\"\n");
17         
18     }
19     
20     private static String cleanUp(String documentation) {
21         // Remove empty lines
22         int end = documentation.length();
23         while(end > 0) {
24             char c = documentation.charAt(end-1);
25             if(c != ' ' && c != '\n')
26                 break;
27             --end;
28         }
29         
30         int begin = 0;
31         for(int i=0;i<end;++i) {
32             char c = documentation.charAt(i);
33             if(c == '\n')
34                 begin = i+1;
35             else if(c != ' ')
36                 break;
37         }
38             
39         // Calculate common indentation
40         int lineStart = begin;
41         int commonIndentation = Integer.MAX_VALUE;
42         for(int i=begin;i<end;++i) {
43             char c = documentation.charAt(i);
44             if(c == '\n') {
45                 lineStart = i+1;
46             }
47             else if(lineStart >= 0 && c != ' ') {
48                 int indentation = i - lineStart;
49                 commonIndentation = Math.min(commonIndentation, indentation);
50                 if(commonIndentation == 0) {
51                     if(begin == 0 && end == documentation.length())
52                         return documentation;
53                     else
54                         return documentation.substring(begin, end);
55                 }
56                 lineStart = -1;
57             }
58         }
59         
60         // Remove common indentation
61         StringBuilder result = new StringBuilder();
62         int charsToSkip = commonIndentation;
63         for(int i=begin;i<end;++i) {
64             char c = documentation.charAt(i);
65             if(c == '\n') {
66                 result.append(c);
67                 charsToSkip = commonIndentation;
68                 continue;
69             }
70             if(charsToSkip > 0)
71                 --charsToSkip;
72             else
73                 result.append(c);
74         }
75         return result.toString();
76     }
77 }