use of org.eclipse.lsp4j.Range in project eclipse.jdt.ls by eclipse.
the class NavigateToDefinitionHandler method definition.
public List<? extends Location> definition(TextDocumentPositionParams position, IProgressMonitor monitor) {
ITypeRoot unit = JDTUtils.resolveTypeRoot(position.getTextDocument().getUri());
Location location = null;
if (unit != null && !monitor.isCanceled()) {
location = computeDefinitionNavigation(unit, position.getPosition().getLine(), position.getPosition().getCharacter(), monitor);
}
if (location == null) {
location = new Location();
location.setRange(new Range());
}
return Arrays.asList(location);
}
use of org.eclipse.lsp4j.Range in project eclipse.jdt.ls by eclipse.
the class WorkspaceDiagnosticsHandler method convertRange.
/**
* @param marker
* @return
*/
private static Range convertRange(IDocument document, IMarker marker) {
int line = marker.getAttribute(IMarker.LINE_NUMBER, -1) - 1;
int cStart = 0;
int cEnd = 0;
try {
// Buildship doesn't provide markers for gradle files, Maven does
if (marker.isSubtypeOf(IMavenConstants.MARKER_ID)) {
cStart = marker.getAttribute(IMavenConstants.MARKER_COLUMN_START, -1);
cEnd = marker.getAttribute(IMavenConstants.MARKER_COLUMN_END, -1);
} else {
int lineOffset = 0;
try {
lineOffset = document.getLineOffset(line);
} catch (BadLocationException unlikelyException) {
JavaLanguageServerPlugin.logException(unlikelyException.getMessage(), unlikelyException);
return new Range(new Position(line, 0), new Position(line, 0));
}
cEnd = marker.getAttribute(IMarker.CHAR_END, -1) - lineOffset;
cStart = marker.getAttribute(IMarker.CHAR_START, -1) - lineOffset;
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
cStart = Math.max(0, cStart);
cEnd = Math.max(0, cEnd);
return new Range(new Position(line, cStart), new Position(line, cEnd));
}
use of org.eclipse.lsp4j.Range in project eclipse.jdt.ls by eclipse.
the class CompletionProposalReplacementProvider method updateReplacement.
/**
* Updates the replacement and any additional replacement for the given item.
*
* @param proposal
* @param item
* @param trigger
*/
public void updateReplacement(CompletionProposal proposal, CompletionItem item, char trigger) {
// reset importRewrite
this.importRewrite = TypeProposalUtils.createImportRewrite(compilationUnit);
List<org.eclipse.lsp4j.TextEdit> additionalTextEdits = new ArrayList<>();
StringBuilder completionBuffer = new StringBuilder();
Range range = null;
if (isSupportingRequiredProposals(proposal)) {
CompletionProposal[] requiredProposals = proposal.getRequiredProposals();
if (requiredProposals != null) {
for (CompletionProposal requiredProposal : requiredProposals) {
switch(requiredProposal.getKind()) {
case CompletionProposal.TYPE_IMPORT:
case CompletionProposal.METHOD_IMPORT:
case CompletionProposal.FIELD_IMPORT:
appendImportProposal(completionBuffer, requiredProposal, proposal.getKind());
break;
case CompletionProposal.TYPE_REF:
org.eclipse.lsp4j.TextEdit edit = toRequiredTypeEdit(requiredProposal, trigger, proposal.canUseDiamond(context));
if (proposal.getKind() == CompletionProposal.CONSTRUCTOR_INVOCATION || proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION || proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_DECLARATION) {
completionBuffer.append(edit.getNewText());
range = edit.getRange();
} else {
additionalTextEdits.add(edit);
}
break;
default:
/*
* In 3.3 we only support the above required proposals, see
* CompletionProposal#getRequiredProposals()
*/
Assert.isTrue(false);
}
}
}
}
if (range == null) {
range = toReplacementRange(proposal);
}
if (proposal.getKind() == CompletionProposal.METHOD_DECLARATION) {
appendMethodOverrideReplacement(completionBuffer, proposal);
} else if (proposal.getKind() == CompletionProposal.POTENTIAL_METHOD_DECLARATION && proposal instanceof GetterSetterCompletionProposal) {
appendMethodPotentialReplacement(completionBuffer, (GetterSetterCompletionProposal) proposal);
} else if (proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION || proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_DECLARATION) {
appendAnonymousClass(completionBuffer, proposal, range);
} else {
appendReplacementString(completionBuffer, proposal);
}
// select insertTextFormat.
if (client.isCompletionSnippetsSupported()) {
item.setInsertTextFormat(InsertTextFormat.Snippet);
} else {
item.setInsertTextFormat(InsertTextFormat.PlainText);
}
String text = completionBuffer.toString();
if (range != null) {
item.setTextEdit(new org.eclipse.lsp4j.TextEdit(range, text));
} else {
// fallback
item.setInsertText(text);
}
addImports(additionalTextEdits);
if (!additionalTextEdits.isEmpty()) {
item.setAdditionalTextEdits(additionalTextEdits);
}
}
use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.
the class DefinitionUtil method getDefinitionPosition.
/**
* Get definition position for the given definition context.
*
* @param definitionContext context of the definition.
* @param lSPackageCache package context for language server.
* @return position
*/
public static List<Location> getDefinitionPosition(TextDocumentServiceContext definitionContext, LSPackageCache lSPackageCache) {
List<Location> contents = new ArrayList<>();
if (definitionContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY) == null) {
return contents;
}
String nodeKind = definitionContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY);
BLangPackage bLangPackage = getPackageOfTheOwner(definitionContext.get(NodeContextKeys.NODE_OWNER_PACKAGE_KEY), definitionContext, lSPackageCache);
BLangNode bLangNode = null;
switch(nodeKind) {
case ContextConstants.FUNCTION:
bLangNode = bLangPackage.functions.stream().filter(function -> function.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
break;
case ContextConstants.STRUCT:
bLangNode = bLangPackage.structs.stream().filter(struct -> struct.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
break;
case ContextConstants.OBJECT:
bLangNode = bLangPackage.objects.stream().filter(object -> object.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
break;
case ContextConstants.ENUM:
bLangNode = bLangPackage.enums.stream().filter(enm -> enm.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
// Fixing the position issue with enum node.
bLangNode.getPosition().eLine = bLangNode.getPosition().sLine;
bLangNode.getPosition().eCol = bLangNode.getPosition().sCol;
break;
case ContextConstants.CONNECTOR:
bLangNode = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
break;
case ContextConstants.ACTION:
bLangNode = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(((BLangInvocation) definitionContext.get(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY)).symbol.owner.name.getValue())).flatMap(connector -> connector.actions.stream()).filter(bAction -> bAction.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
break;
case ContextConstants.TRANSFORMER:
bLangNode = bLangPackage.transformers.stream().filter(bTransformer -> bTransformer.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
break;
case ContextConstants.ENDPOINT:
bLangNode = bLangPackage.globalEndpoints.stream().filter(globalEndpoint -> globalEndpoint.name.value.equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
if (bLangNode == null) {
DefinitionTreeVisitor definitionTreeVisitor = new DefinitionTreeVisitor(definitionContext);
definitionContext.get(NodeContextKeys.NODE_STACK_KEY).pop().accept(definitionTreeVisitor);
if (definitionContext.get(NodeContextKeys.NODE_KEY) != null) {
bLangNode = definitionContext.get(NodeContextKeys.NODE_KEY);
}
}
break;
case ContextConstants.VARIABLE:
bLangNode = bLangPackage.globalVars.stream().filter(globalVar -> globalVar.name.getValue().equals(definitionContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
// BLangNode is null only when node at the cursor position is a local variable.
if (bLangNode == null) {
DefinitionTreeVisitor definitionTreeVisitor = new DefinitionTreeVisitor(definitionContext);
definitionContext.get(NodeContextKeys.NODE_STACK_KEY).pop().accept(definitionTreeVisitor);
if (definitionContext.get(NodeContextKeys.NODE_KEY) != null) {
bLangNode = definitionContext.get(NodeContextKeys.NODE_KEY);
break;
}
}
break;
default:
break;
}
if (bLangNode == null) {
return contents;
}
Location l = new Location();
TextDocumentPositionParams position = definitionContext.get(DocumentServiceKeys.POSITION_KEY);
Path parentPath = CommonUtil.getPath(new LSDocument(position.getTextDocument().getUri())).getParent();
if (parentPath != null) {
String fileName = bLangNode.getPosition().getSource().getCompilationUnitName();
Path filePath = Paths.get(CommonUtil.getPackageURI(definitionContext.get(NodeContextKeys.PACKAGE_OF_NODE_KEY).name.getValue(), parentPath.toString(), definitionContext.get(NodeContextKeys.PACKAGE_OF_NODE_KEY).name.getValue()), fileName);
l.setUri(filePath.toUri().toString());
Range r = new Range();
// Subtract 1 to convert the token lines and char positions to zero based indexing
r.setStart(new Position(bLangNode.getPosition().getStartLine() - 1, bLangNode.getPosition().getStartColumn() - 1));
r.setEnd(new Position(bLangNode.getPosition().getEndLine() - 1, bLangNode.getPosition().getEndColumn() - 1));
l.setRange(r);
contents.add(l);
}
return contents;
}
use of org.eclipse.lsp4j.Range in project ballerina by ballerina-lang.
the class ReferencesTreeVisitor method getLocation.
/**
* Get the physical source location of the given package.
*
* @param bLangNode ballerina language node references are requested for
* @param ownerPackageName list of name compositions of the node's package name
* @param currentPackageName list of name compositions of the current package
* @return location of the package of the given node
*/
private Location getLocation(BLangNode bLangNode, String ownerPackageName, String currentPackageName) {
Location l = new Location();
Range r = new Range();
TextDocumentPositionParams position = this.context.get(DocumentServiceKeys.POSITION_KEY);
Path parentPath = CommonUtil.getPath(new LSDocument(position.getTextDocument().getUri())).getParent();
if (parentPath != null) {
String fileName = bLangNode.getPosition().getSource().getCompilationUnitName();
Path filePath = Paths.get(CommonUtil.getPackageURI(currentPackageName, parentPath.toString(), ownerPackageName), fileName);
l.setUri(filePath.toUri().toString());
// Subtract 1 to convert the token lines and char positions to zero based indexing
r.setStart(new Position(bLangNode.getPosition().getStartLine() - 1, bLangNode.getPosition().getStartColumn() - 1));
r.setEnd(new Position(bLangNode.getPosition().getEndLine() - 1, bLangNode.getPosition().getEndColumn() - 1));
l.setRange(r);
}
return l;
}
Aggregations