use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.
the class SymbolFindingVisitor method getRange.
private Range getRange(BLangNode node) {
Range r = new Range();
// LSP range is 0 based
int startLine = node.getPosition().getStartLine() - 1;
int startChar = node.getPosition().getStartColumn() - 1;
int endLine = node.getPosition().getEndLine() - 1;
int endChar = node.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));
return r;
}
use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.
the class CommandExecutor method executeAddDocumentation.
/**
* Execute the add documentation command.
* @param context Workspace service context
*/
private static void executeAddDocumentation(WorkspaceServiceContext context) {
String topLevelNodeType = "";
String documentUri = "";
int line = 0;
VersionedTextDocumentIdentifier textDocumentIdentifier = new VersionedTextDocumentIdentifier();
for (Object arg : context.get(ExecuteCommandKeys.COMMAND_ARGUMENTS_KEY)) {
if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_DOC_URI)) {
documentUri = (String) ((LinkedTreeMap) arg).get(ARG_VALUE);
textDocumentIdentifier.setUri(documentUri);
context.put(DocumentServiceKeys.FILE_URI_KEY, documentUri);
} else if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_NODE_TYPE)) {
topLevelNodeType = (String) ((LinkedTreeMap) arg).get(ARG_VALUE);
} else if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_NODE_LINE)) {
line = Integer.parseInt((String) ((LinkedTreeMap) arg).get(ARG_VALUE));
}
}
BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(context, context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY), false, LSCustomErrorStrategy.class, false).get(0);
CommandUtil.DocAttachmentInfo docAttachmentInfo = getDocumentEditForNodeByPosition(topLevelNodeType, bLangPackage, line);
if (docAttachmentInfo != null) {
String fileContent = context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY).getFileContent(Paths.get(URI.create(documentUri)));
String[] contentComponents = fileContent.split(System.lineSeparator());
int replaceEndCol = contentComponents[line - 1].length();
String replaceText = String.join(System.lineSeparator(), Arrays.asList(Arrays.copyOfRange(contentComponents, 0, line))) + System.lineSeparator() + docAttachmentInfo.getDocAttachment();
Range range = new Range(new Position(0, 0), new Position(line - 1, replaceEndCol));
applySingleTextEdit(replaceText, range, textDocumentIdentifier, context.get(ExecuteCommandKeys.LANGUAGE_SERVER_KEY).getClient());
}
}
use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.
the class CommandExecutor method executeImportPackage.
/**
* Execute the command, import package.
* @param context Workspace service context
*/
private static void executeImportPackage(WorkspaceServiceContext context) {
String documentUri = null;
VersionedTextDocumentIdentifier textDocumentIdentifier = new VersionedTextDocumentIdentifier();
for (Object arg : context.get(ExecuteCommandKeys.COMMAND_ARGUMENTS_KEY)) {
if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_DOC_URI)) {
documentUri = (String) ((LinkedTreeMap) arg).get(ARG_VALUE);
textDocumentIdentifier.setUri(documentUri);
context.put(DocumentServiceKeys.FILE_URI_KEY, documentUri);
} else if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_PKG_NAME)) {
context.put(ExecuteCommandKeys.PKG_NAME_KEY, (String) ((LinkedTreeMap) arg).get(ARG_VALUE));
}
}
if (documentUri != null && context.get(ExecuteCommandKeys.PKG_NAME_KEY) != null) {
String fileContent = context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY).getFileContent(Paths.get(URI.create(documentUri)));
String[] contentComponents = fileContent.split("\\n|\\r\\n|\\r");
int totalLines = contentComponents.length;
int lastNewLineCharIndex = Math.max(fileContent.lastIndexOf("\n"), fileContent.lastIndexOf("\r"));
int lastCharCol = fileContent.substring(lastNewLineCharIndex + 1).length();
BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(context, context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY), false, LSCustomErrorStrategy.class, false).get(0);
context.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, bLangPackage.symbol.getName().getValue());
String pkgName = context.get(ExecuteCommandKeys.PKG_NAME_KEY);
DiagnosticPos pos;
// Filter the imports except the runtime import
List<BLangImportPackage> imports = bLangPackage.getImports().stream().filter(bLangImportPackage -> !bLangImportPackage.getAlias().toString().equals(RUNTIME_PKG_ALIAS)).collect(Collectors.toList());
if (!imports.isEmpty()) {
BLangImportPackage lastImport = bLangPackage.getImports().get(bLangPackage.getImports().size() - 1);
pos = lastImport.getPosition();
} else if (imports.isEmpty() && bLangPackage.getPackageDeclaration() != null) {
pos = (DiagnosticPos) bLangPackage.getPackageDeclaration().getPosition();
} else {
pos = null;
}
int endCol = pos == null ? -1 : pos.getEndColumn() - 1;
int endLine = pos == null ? 0 : pos.getEndLine() - 1;
String remainingTextToReplace;
if (endCol != -1) {
int contentLengthToReplaceStart = fileContent.substring(0, fileContent.indexOf(contentComponents[endLine])).length() + endCol + 1;
remainingTextToReplace = fileContent.substring(contentLengthToReplaceStart);
} else {
remainingTextToReplace = fileContent;
}
String editText = (pos != null ? "\r\n" : "") + "import " + pkgName + ";" + (remainingTextToReplace.startsWith("\n") || remainingTextToReplace.startsWith("\r") ? "" : "\r\n") + remainingTextToReplace;
Range range = new Range(new Position(endLine, endCol + 1), new Position(totalLines + 1, lastCharCol));
applySingleTextEdit(editText, range, textDocumentIdentifier, context.get(ExecuteCommandKeys.LANGUAGE_SERVER_KEY).getClient());
}
}
use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.
the class CommandExecutor method getTextEdit.
/**
* Get TextEdit from doc attachment info.
* @param attachmentInfo Doc attachment info
* @param contentComponents file content component
* @return {@link TextEdit} Text edit for attachment info
*/
private static TextEdit getTextEdit(CommandUtil.DocAttachmentInfo attachmentInfo, String[] contentComponents) {
int replaceFrom = attachmentInfo.getReplaceStartFrom();
int replaceEndCol = contentComponents[attachmentInfo.getReplaceStartFrom()].length();
Range range = new Range(new Position(replaceFrom, 0), new Position(replaceFrom, replaceEndCol));
String replaceText = attachmentInfo.getDocAttachment() + System.lineSeparator() + contentComponents[replaceFrom];
return new TextEdit(range, replaceText);
}
use of org.eclipse.lsp4j.Range in project sts4 by spring-projects.
the class VscodeHoverEngineAdapter method handle.
@Override
public Hover handle(TextDocumentPositionParams params) {
// a trivial pre-resolved future.
try {
SimpleTextDocumentService documents = server.getTextDocumentService();
TextDocument doc = documents.get(params);
if (doc != null) {
int offset = doc.toOffset(params.getPosition());
Tuple2<Renderable, IRegion> hoverTuple = hoverInfoProvider.getHoverInfo(doc, offset);
if (hoverTuple != null) {
Renderable hoverInfo = hoverTuple.getT1();
IRegion region = hoverTuple.getT2();
Range range = doc.toRange(region.getOffset(), region.getLength());
String rendered = render(hoverInfo, type);
if (StringUtil.hasText(rendered)) {
Hover hover = new Hover(ImmutableList.of(Either.forLeft(rendered)), range);
return hover;
}
}
}
} catch (Exception e) {
logger.error("error computing hover", e);
}
return SimpleTextDocumentService.NO_HOVER;
}
Aggregations