use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.
the class BallerinaTextDocumentService method publishDiagnostics.
private void publishDiagnostics(List<org.ballerinalang.util.diagnostic.Diagnostic> balDiagnostics, Path path) {
Map<String, List<Diagnostic>> diagnosticsMap = new HashMap<>();
balDiagnostics.forEach(diagnostic -> {
Diagnostic d = new Diagnostic();
d.setSeverity(DiagnosticSeverity.Error);
d.setMessage(diagnostic.getMessage());
Range r = new Range();
// LSP diagnostics range is 0 based
int startLine = diagnostic.getPosition().getStartLine() - 1;
int startChar = diagnostic.getPosition().getStartColumn() - 1;
int endLine = diagnostic.getPosition().getEndLine() - 1;
int endChar = diagnostic.getPosition().getEndColumn() - 1;
if (endLine <= 0) {
endLine = startLine;
}
if (endChar <= 0) {
endChar = startChar + 1;
}
r.setStart(new Position(startLine, startChar));
r.setEnd(new Position(endLine, endChar));
d.setRange(r);
String fileName = diagnostic.getPosition().getSource().getCompilationUnitName();
Path filePath = Paths.get(path.getParent().toString(), fileName);
String fileURI = filePath.toUri().toString();
if (!diagnosticsMap.containsKey(fileURI)) {
diagnosticsMap.put(fileURI, new ArrayList<Diagnostic>());
}
List<Diagnostic> clientDiagnostics = diagnosticsMap.get(fileURI);
clientDiagnostics.add(d);
});
// clear previous diagnostics
List<Diagnostic> empty = new ArrayList<Diagnostic>(0);
for (Map.Entry<String, List<Diagnostic>> entry : lastDiagnosticMap.entrySet()) {
if (diagnosticsMap.containsKey(entry.getKey())) {
continue;
}
PublishDiagnosticsParams diagnostics = new PublishDiagnosticsParams();
diagnostics.setUri(entry.getKey());
diagnostics.setDiagnostics(empty);
this.ballerinaLanguageServer.getClient().publishDiagnostics(diagnostics);
}
for (Map.Entry<String, List<Diagnostic>> entry : diagnosticsMap.entrySet()) {
PublishDiagnosticsParams diagnostics = new PublishDiagnosticsParams();
diagnostics.setUri(entry.getKey());
diagnostics.setDiagnostics(entry.getValue());
this.ballerinaLanguageServer.getClient().publishDiagnostics(diagnostics);
}
lastDiagnosticMap = diagnosticsMap;
}
use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.
the class BallerinaTextDocumentService method formatting.
@Override
public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) {
return CompletableFuture.supplyAsync(() -> {
String textEditContent = null;
TextDocumentServiceContext formatContext = new TextDocumentServiceContext();
formatContext.put(DocumentServiceKeys.FILE_URI_KEY, params.getTextDocument().getUri());
LSDocument document = new LSDocument(params.getTextDocument().getUri());
String fileContent = documentManager.getFileContent(CommonUtil.getPath(document));
String[] contentComponents = fileContent.split("\\n|\\r\\n|\\r");
int lastNewLineCharIndex = Math.max(fileContent.lastIndexOf("\n"), fileContent.lastIndexOf("\r"));
int lastCharCol = fileContent.substring(lastNewLineCharIndex + 1).length();
int totalLines = contentComponents.length;
Range range = new Range(new Position(0, 0), new Position(totalLines, lastCharCol));
try {
// Source generation for given ast.
JsonObject ast = TextDocumentFormatUtil.getAST(params, documentManager, formatContext);
SourceGen sourceGen = new SourceGen(0);
sourceGen.build(ast.getAsJsonObject("model"), null, "CompilationUnit");
textEditContent = sourceGen.getSourceOf(ast.getAsJsonObject("model"), true, false);
} catch (Exception e) {
// Ignore
}
TextEdit textEdit = textEditContent != null ? new TextEdit(range, textEditContent) : null;
return Collections.singletonList(textEdit);
});
}
use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.
the class RenameUtil method getRenameTextEdits.
/**
* Get the list of rename related TextEdits.
*
* @param locationList List of locations of occurrences
* @param documentManager {@link WorkspaceDocumentManager} instance
* @param newName New name to be replaced with
* @param replaceSymbolName Symbol name being replaced
* @return {@link List} List of TextEdits
*/
public static List<TextDocumentEdit> getRenameTextEdits(List<Location> locationList, WorkspaceDocumentManager documentManager, String newName, String replaceSymbolName) {
Map<String, ArrayList<Location>> documentLocationMap = new HashMap<>();
List<TextDocumentEdit> documentEdits = new ArrayList<>();
Comparator<Location> locationComparator = (location1, location2) -> location1.getRange().getStart().getCharacter() - location2.getRange().getStart().getCharacter();
locationList.forEach(location -> {
if (documentLocationMap.containsKey(location.getUri())) {
documentLocationMap.get(location.getUri()).add(location);
} else {
documentLocationMap.put(location.getUri(), (ArrayList<Location>) Lists.of(location));
}
});
documentLocationMap.forEach((uri, locations) -> {
Collections.sort(locations, locationComparator);
String fileContent = documentManager.getFileContent(CommonUtil.getPath(new LSDocument(uri)));
String[] contentComponents = fileContent.split("\\n|\\r\\n|\\r");
int lastNewLineCharIndex = Math.max(fileContent.lastIndexOf("\n"), fileContent.lastIndexOf("\r"));
int lastCharCol = fileContent.substring(lastNewLineCharIndex + 1).length();
for (Location location : locations) {
int line = location.getRange().getStart().getLine();
StringBuilder lineComponent = new StringBuilder(contentComponents[line]);
int index = lineComponent.indexOf(replaceSymbolName);
while (index >= 0) {
char previousChar = lineComponent.charAt(index - 1);
if (Character.isLetterOrDigit(previousChar) || String.valueOf(previousChar).equals("_")) {
index = lineComponent.indexOf(replaceSymbolName, index + replaceSymbolName.length());
} else {
lineComponent.replace(index, index + replaceSymbolName.length(), newName);
index = lineComponent.indexOf(replaceSymbolName, index + newName.length());
}
}
contentComponents[line] = lineComponent.toString();
}
Range range = new Range(new Position(0, 0), new Position(contentComponents.length, lastCharCol));
TextEdit textEdit = new TextEdit(range, String.join("\r\n", Arrays.asList(contentComponents)));
VersionedTextDocumentIdentifier textDocumentIdentifier = new VersionedTextDocumentIdentifier();
textDocumentIdentifier.setUri(uri);
TextDocumentEdit textDocumentEdit = new TextDocumentEdit(textDocumentIdentifier, Collections.singletonList(textEdit));
documentEdits.add(textDocumentEdit);
});
return documentEdits;
}
use of org.eclipse.lsp4j.Range in project freemarker-languageserver by angelozerr.
the class FreemarkerTextDocumentService method validateFMDocument.
private List<Diagnostic> validateFMDocument(String xmlDocumentUri, String xmlDocumentContent) {
List<Diagnostic> diagnostics = new ArrayList<Diagnostic>();
if (fmConfiguration == null) {
fmConfiguration = new Configuration(Configuration.getVersion());
fmConfiguration.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
fmConfiguration.setTabSize(1);
}
try {
@SuppressWarnings("unused") Template dummy = new Template(xmlDocumentUri, xmlDocumentContent, fmConfiguration);
} catch (ParseException e) {
Position start = new Position(e.getLineNumber() - 1, e.getColumnNumber());
Position end = new Position(e.getEndLineNumber() - 1, e.getEndColumnNumber());
Diagnostic diagnostic = new Diagnostic(new Range(start, end), e.getEditorMessage());
diagnostics.add(diagnostic);
} catch (IOException e) {
e.printStackTrace();
}
return diagnostics;
}
use of org.eclipse.lsp4j.Range in project sts4 by spring-projects.
the class Editor method assertHighlights.
public List<Range> assertHighlights(String... expectedHighlights) throws Exception {
HighlightParams highlights = harness.getHighlights(doc);
List<Range> ranges = new ArrayList<>(highlights.getRanges());
Collections.sort(ranges, RANGE_COMPARATOR);
List<String> actualHighlights = ranges.stream().map(this::getText).collect(Collectors.toList());
assertEquals(ImmutableList.copyOf(expectedHighlights), actualHighlights);
return ranges;
}
Aggregations