13
13
import java .util .Set ;
14
14
import java .util .stream .Collectors ;
15
15
import java .util .stream .IntStream ;
16
+
16
17
import org .eclipse .lsp4j .CompletionItem ;
17
18
import org .eclipse .lsp4j .CompletionItemKind ;
18
19
import org .eclipse .lsp4j .Diagnostic ;
19
20
import org .eclipse .lsp4j .DiagnosticSeverity ;
20
21
import org .eclipse .lsp4j .InsertTextFormat ;
22
+ import org .eclipse .lsp4j .Location ;
21
23
import org .eclipse .lsp4j .jsonrpc .CompletableFutures ;
22
24
import org .eclipse .lsp4j .Position ;
23
25
import org .eclipse .lsp4j .PublishDiagnosticsParams ;
24
26
import org .eclipse .lsp4j .Range ;
25
27
import org .eclipse .lsp4j .services .LanguageClient ;
26
28
import org .eclipse .lsp4j .TextEdit ;
27
29
import org .jsoup .Jsoup ;
30
+
28
31
import processing .app .Base ;
29
32
import processing .app .contrib .ModeContribution ;
30
33
import processing .app .Platform ;
41
44
import processing .mode .java .PreprocService ;
42
45
import processing .mode .java .PreprocSketch ;
43
46
47
+ import static java .util .Arrays .copyOfRange ;
44
48
45
49
class PdeAdapter {
46
50
File rootPath ;
@@ -104,6 +108,41 @@ static Offset toLineCol(String s, int offset) {
104
108
return new Offset (line , col );
105
109
}
106
110
111
+
112
+ /**
113
+ * Converts a tabOffset to a position within a tab
114
+ * @param program current code(text) from a tab
115
+ * @param tabOffset character offset inside a tab
116
+ * @return Position(line and col) within the tab
117
+ */
118
+ static Position toPosition (String program , int tabOffset ){
119
+ Offset offset = toLineCol (program , tabOffset );
120
+ return new Position (offset .line , offset .col -1 );
121
+ }
122
+
123
+
124
+ /**
125
+ * Converts a range (start to end offset) to a location.
126
+ * @param program current code(text) from a tab
127
+ * @param startTabOffset starting character offset inside a tab
128
+ * @param stopTabOffset ending character offset inside a tab
129
+ * @param uri uri from a tab
130
+ * @return Range inside a file
131
+ */
132
+ static Location toLocation (
133
+ String program ,
134
+ int startTabOffset ,
135
+ int stopTabOffset ,
136
+ URI uri
137
+ ){
138
+ Position startPos = toPosition (program , startTabOffset );
139
+ Position stopPos = toPosition (program , stopTabOffset );
140
+
141
+ Range range = new Range (startPos , stopPos );
142
+ return new Location (uri .toString (), range );
143
+ }
144
+
145
+
107
146
static void init () {
108
147
Base .setCommandLine ();
109
148
Platform .init ();
@@ -126,6 +165,59 @@ Optional<SketchCode> findCodeByUri(URI uri) {
126
165
);
127
166
}
128
167
168
+
169
+ /**
170
+ * Looks for the tab number for a given text
171
+ * @param code text(code) from a tab
172
+ * @return tabIndex where the code belongs to, or empty
173
+ */
174
+ public Optional <Integer > findTabIndex (SketchCode code ){
175
+ int tabsCount = sketch .getCodeCount ();
176
+ java .util .OptionalInt optionalTabIndex ;
177
+ optionalTabIndex = IntStream .range (0 , tabsCount )
178
+ .filter (i -> sketch .getCode (i ).equals (code ))
179
+ .findFirst ();
180
+
181
+ if (optionalTabIndex .isEmpty ()){
182
+ return Optional .empty ();
183
+ }
184
+
185
+ return Optional .of (optionalTabIndex .getAsInt ());
186
+ }
187
+
188
+
189
+ /**
190
+ * Looks for the javaOffset, this offset is the character position inside the
191
+ * full java file. The position can be used by the AST to find a node.
192
+ * @param uri uri of the file(tab) where to look
193
+ * @param line line number
194
+ * @param col column number
195
+ * @return character offset within the full AST
196
+ */
197
+ public Optional <Integer > findJavaOffset (URI uri , int line , int col ){
198
+
199
+ Optional <SketchCode > optionalCode = findCodeByUri (uri );
200
+ if (optionalCode .isEmpty ()){
201
+ System .out .println ("couldn't find sketch code" );
202
+ return Optional .empty ();
203
+ }
204
+ SketchCode code = optionalCode .get ();
205
+
206
+ Optional <Integer > optionalTabIndex = findTabIndex (code );
207
+ if (optionalTabIndex .isEmpty ()){
208
+ System .out .println ("couldn't find tab index" );
209
+ return Optional .empty ();
210
+ }
211
+ int tabIndex = optionalTabIndex .get ();
212
+
213
+ String [] codeLines = copyOfRange (code .getProgram ().split ("\n " ), 0 ,line );
214
+ String codeString = String .join ("\n " , codeLines );
215
+ int tabOffset = codeString .length () + col ;
216
+
217
+ return Optional .of (ps .tabOffsetToJavaOffset (tabIndex , tabOffset ));
218
+ }
219
+
220
+
129
221
void updateProblems (List <Problem > problems ) {
130
222
Map <URI , List <Diagnostic >> dias = problems .stream ()
131
223
.map (prob -> {
0 commit comments