Skip to content

Commit 274664c

Browse files
committed
Add helper functions
1 parent ae80913 commit 274664c

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

java/src/processing/mode/java/lsp/PdeAdapter.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@
1313
import java.util.Set;
1414
import java.util.stream.Collectors;
1515
import java.util.stream.IntStream;
16+
1617
import org.eclipse.lsp4j.CompletionItem;
1718
import org.eclipse.lsp4j.CompletionItemKind;
1819
import org.eclipse.lsp4j.Diagnostic;
1920
import org.eclipse.lsp4j.DiagnosticSeverity;
2021
import org.eclipse.lsp4j.InsertTextFormat;
22+
import org.eclipse.lsp4j.Location;
2123
import org.eclipse.lsp4j.jsonrpc.CompletableFutures;
2224
import org.eclipse.lsp4j.Position;
2325
import org.eclipse.lsp4j.PublishDiagnosticsParams;
2426
import org.eclipse.lsp4j.Range;
2527
import org.eclipse.lsp4j.services.LanguageClient;
2628
import org.eclipse.lsp4j.TextEdit;
2729
import org.jsoup.Jsoup;
30+
2831
import processing.app.Base;
2932
import processing.app.contrib.ModeContribution;
3033
import processing.app.Platform;
@@ -41,6 +44,7 @@
4144
import processing.mode.java.PreprocService;
4245
import processing.mode.java.PreprocSketch;
4346

47+
import static java.util.Arrays.copyOfRange;
4448

4549
class PdeAdapter {
4650
File rootPath;
@@ -104,6 +108,41 @@ static Offset toLineCol(String s, int offset) {
104108
return new Offset(line, col);
105109
}
106110

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+
107146
static void init() {
108147
Base.setCommandLine();
109148
Platform.init();
@@ -126,6 +165,59 @@ Optional<SketchCode> findCodeByUri(URI uri) {
126165
);
127166
}
128167

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+
129221
void updateProblems(List<Problem> problems) {
130222
Map<URI, List<Diagnostic>> dias = problems.stream()
131223
.map(prob -> {

0 commit comments

Comments
 (0)