use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.
the class VscodeCompletionEngineAdapter method adaptEdits.
private static Optional<TextEdit> adaptEdits(TextDocument doc, DocumentEdits edits) {
try {
TextReplace replaceEdit = edits.asReplacement(doc);
if (replaceEdit == null) {
// The original edit does nothing.
return Optional.empty();
} else {
TextDocument newDoc = doc.copy();
edits.apply(newDoc);
TextEdit vscodeEdit = new TextEdit();
vscodeEdit.setRange(doc.toRange(replaceEdit.start, replaceEdit.end - replaceEdit.start));
if (Boolean.getBoolean("lsp.completions.indentation.enable")) {
vscodeEdit.setNewText(replaceEdit.newText);
} else {
vscodeEdit.setNewText(vscodeIndentFix(doc, vscodeEdit.getRange().getStart(), replaceEdit.newText));
}
// TODO: cursor offset within newText? for now we assume its always at the end.
return Optional.of(vscodeEdit);
}
} catch (Exception e) {
LOG.get().error("{}", e);
return Optional.empty();
}
}
use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.
the class SimpleDefinitionFinder method findDefinitions.
/**
* This is meant to be overridden by subclass. This method provides a simple implementation
* of 'goto definition' (which is not one you probably want to use in practice, but it
* might be usful just to test whether things are wired up correctly to make the
* 'goto definition' action in vscode work.
* <p>
* The implementation provided here simply looks for the first occurrence of the word
* currently pointed at in the current document using String.indexOf.
*/
protected Flux<Location> findDefinitions(TextDocumentPositionParams params) {
try {
TextDocument doc = server.getTextDocumentService().get(params);
if (doc != null) {
int offset = doc.toOffset(params.getPosition());
int start = offset;
while (Character.isLetter(doc.getSafeChar(start))) {
start--;
}
start = start + 1;
int end = offset;
while (Character.isLetter(doc.getSafeChar(end))) {
end++;
}
String word = doc.textBetween(start, end);
Log.log("Looking for definition of '" + word + "'");
String text = doc.get();
int def = text.indexOf(word);
if (def >= 0) {
return Flux.just(new Location(params.getTextDocument().getUri(), doc.toRange(def, word.length()))).doOnNext((Location loc) -> {
Log.log("definition: " + loc);
});
}
}
} catch (Exception e) {
Log.log(e);
}
return Flux.empty();
}
use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.
the class BootJavaCodeLensEngine method handle.
@Override
public List<? extends CodeLens> handle(CodeLensParams params) {
SimpleTextDocumentService documents = server.getTextDocumentService();
String docURI = params.getTextDocument().getUri();
if (documents.get(docURI) != null) {
TextDocument doc = documents.get(docURI).copy();
try {
List<? extends CodeLens> codeLensesResult = provideCodeLenses(doc);
if (codeLensesResult != null) {
return codeLensesResult;
}
} catch (Exception e) {
}
}
return SimpleTextDocumentService.NO_CODELENS;
}
use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.
the class CompilationUnitCacheTest method cu_cache_invalidated_by_project_change.
@Test
public void cu_cache_invalidated_by_project_change() throws Exception {
File directory = new File(ProjectsHarness.class.getResource("/test-projects/test-request-mapping-live-hover/").toURI());
String docUri = directory.toPath().resolve("src/main/java/example/HelloWorldController.java").toUri().toString();
harness.intialize(directory);
URI fileUri = new URI(docUri);
Path path = Paths.get(fileUri);
String content = new String(Files.readAllBytes(path));
TextDocument document = new TextDocument(docUri, LanguageId.JAVA, 0, content);
CompilationUnit cu = getCompilationUnit(document);
assertNotNull(cu);
CompilationUnit cuAnother = getCompilationUnit(document);
assertTrue(cu == cuAnother);
harness.changeFile(directory.toPath().resolve(MavenCore.POM_XML).toUri().toString());
cuAnother = getCompilationUnit(document);
assertNotNull(cuAnother);
assertFalse(cu == cuAnother);
}
use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.
the class CompilationUnitCacheTest method cu_cache_invalidated_by_project_deletion.
@Test
public void cu_cache_invalidated_by_project_deletion() throws Exception {
File directory = new File(ProjectsHarness.class.getResource("/test-projects/test-request-mapping-live-hover/").toURI());
String docUri = directory.toPath().resolve("src/main/java/example/HelloWorldController.java").toUri().toString();
harness.intialize(directory);
URI fileUri = new URI(docUri);
Path path = Paths.get(fileUri);
String content = new String(Files.readAllBytes(path));
TextDocument document = new TextDocument(docUri, LanguageId.JAVA, 0, content);
CompilationUnit cu = getCompilationUnit(document);
assertNotNull(cu);
CompilationUnit cuAnother = getCompilationUnit(document);
assertTrue(cu == cuAnother);
harness.deleteFile(directory.toPath().resolve(MavenCore.POM_XML).toUri().toString());
cuAnother = getCompilationUnit(document);
assertNotNull(cuAnother);
assertFalse(cu == cuAnother);
}
Aggregations