use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.
the class ValuePropertyReferencesProvider method findReferencesInYMLFile.
private List<Location> findReferencesInYMLFile(String filePath, String propertyKey) {
List<Location> foundLocations = new ArrayList<>();
try {
String fileContent = FileUtils.readFileToString(new File(filePath));
Yaml yaml = new Yaml();
YamlASTProvider parser = new YamlParser(yaml);
URI docURI = Paths.get(filePath).toUri();
TextDocument doc = new TextDocument(docURI.toString(), null);
doc.setText(fileContent);
YamlFileAST ast = parser.getAST(doc);
List<Node> nodes = ast.getNodes();
if (nodes != null && !nodes.isEmpty()) {
for (Node node : nodes) {
Node foundNode = findNode(node, "", propertyKey);
if (foundNode != null) {
Position start = new Position();
start.setLine(foundNode.getStartMark().getLine());
start.setCharacter(foundNode.getStartMark().getColumn());
Position end = new Position();
end.setLine(foundNode.getEndMark().getLine());
end.setCharacter(foundNode.getEndMark().getColumn());
Range range = new Range();
range.setStart(start);
range.setEnd(end);
Location location = new Location(docURI.toString(), range);
foundLocations.add(location);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return foundLocations;
}
use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.
the class BootPropertiesLanguageServerComponents method getCompletionEngine.
@Override
public ICompletionEngine getCompletionEngine() {
ICompletionEngine propertiesCompletions = new SpringPropertiesCompletionEngine(indexProvider, typeUtilProvider, javaProjectFinder);
ICompletionEngine yamlCompletions = new YamlCompletionEngine(yamlStructureProvider, yamlAssistContextProvider, COMPLETION_OPTIONS);
return (TextDocument document, int offset) -> {
String uri = document.getUri();
if (uri != null) {
if (uri.endsWith(PROPERTIES)) {
return propertiesCompletions.getCompletions(document, offset);
} else if (uri.endsWith(YML)) {
return yamlCompletions.getCompletions(document, offset);
}
}
return ImmutableList.of();
};
}
use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.
the class CompilationUnitCache method getClasspathEntries.
private static String[] getClasspathEntries(TextDocument document, IJavaProject project) throws Exception {
if (project == null) {
return new String[0];
} else {
IClasspath classpath = project.getClasspath();
Stream<Path> classpathEntries = classpath.getClasspathEntries().stream();
return classpathEntries.filter(path -> path.toFile().exists()).map(path -> path.toAbsolutePath().toString()).toArray(String[]::new);
}
}
use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.
the class SimpleLanguageServer method validateWith.
/**
* Convenience method. Subclasses can call this to use a {@link IReconcileEngine} ported
* from old STS codebase to validate a given {@link TextDocument} and publish Diagnostics.
*/
public void validateWith(TextDocumentIdentifier docId, IReconcileEngine engine) {
CompletableFuture<Void> reconcileSession = this.busyReconcile = new CompletableFuture<Void>();
// Log.debug("Reconciling BUSY");
SimpleTextDocumentService documents = getTextDocumentService();
int requestedVersion = documents.getDocument(docId.getUri()).getVersion();
// Avoid running in the same thread as lsp4j as it can result
// in long "hangs" for slow reconcile providers
Mono.fromRunnable(() -> {
TextDocument doc = documents.getDocument(docId.getUri()).copy();
if (requestedVersion != doc.getVersion()) {
// Do not bother reconciling if document contents is already stale.
return;
}
if (testListener != null) {
testListener.reconcileStarted(docId.getUri(), doc.getVersion());
}
IProblemCollector problems = new IProblemCollector() {
private LinkedHashSet<Diagnostic> diagnostics = new LinkedHashSet<>();
private List<Quickfix> quickfixes = new ArrayList<>();
@Override
public void endCollecting() {
documents.setQuickfixes(docId, quickfixes);
documents.publishDiagnostics(docId, diagnostics);
}
@Override
public void beginCollecting() {
diagnostics.clear();
}
@Override
public void checkPointCollecting() {
// publish what has been collected so far
documents.publishDiagnostics(docId, diagnostics);
}
@Override
public void accept(ReconcileProblem problem) {
try {
DiagnosticSeverity severity = getDiagnosticSeverity(problem);
if (severity != null) {
Diagnostic d = new Diagnostic();
d.setCode(problem.getCode());
d.setMessage(problem.getMessage());
Range rng = doc.toRange(problem.getOffset(), problem.getLength());
d.setRange(rng);
d.setSeverity(severity);
List<QuickfixData<?>> fixes = problem.getQuickfixes();
if (CollectionUtil.hasElements(fixes)) {
for (QuickfixData<?> fix : fixes) {
quickfixes.add(new Quickfix<>(CODE_ACTION_COMMAND_ID, d, fix));
}
}
diagnostics.add(d);
}
} catch (BadLocationException e) {
Log.warn("Invalid reconcile problem ignored", e);
}
}
};
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// }
engine.reconcile(doc, problems);
}).onErrorResume(error -> {
Log.log(error);
return Mono.empty();
}).doFinally(ignore -> {
reconcileSession.complete(null);
// Log.debug("Reconciler DONE : "+this.busyReconcile.isDone());
}).subscribeOn(RECONCILER_SCHEDULER).subscribe();
}
use of org.springframework.ide.vscode.commons.util.text.TextDocument in project sts4 by spring-projects.
the class SimpleTextDocumentService method didSave.
@Override
public void didSave(DidSaveTextDocumentParams params) {
// Workaround for PT 147263283, where error markers in STS are lost on document save.
// STS 3.9.0 does not use the LSP4E editor for edit manifest.yml, which correctly retains error markers after save.
// Instead, because the LSP4E editor is missing support for hovers and completions, STS 3.9.0 uses its own manifest editor
// which extends the YEdit editor. This YEdit editor has a problem, where on save, all error markers are deleted.
// When STS uses the LSP4E editor and no longer needs its own YEdit-based editor, the issue with error markers disappearing
// on save should not be a problem anymore, and the workaround below will no longer be needed.
async.execute(() -> {
if (documentSaveListener != null) {
TextDocumentIdentifier docId = params.getTextDocument();
String url = docId.getUri();
Log.debug("didSave: " + url);
if (url != null) {
TextDocument doc = getDocument(url);
documentSaveListener.accept(new TextDocumentSaveChange(doc));
}
}
});
}
Aggregations