use of com.redhat.qute.ls.commons.BadLocationException in project quarkus-ls by redhat-developer.
the class QuteCompletions method doComplete.
/**
* Returns completion list for the given position
*
* @param template the Qute template
* @param position the position where completion was triggered
* @param completionSettings the completion settings.
* @param formattingSettings the formatting settings.
* @param cancelChecker the cancel checker
* @return completion list for the given position
*/
public CompletableFuture<CompletionList> doComplete(Template template, Position position, QuteCompletionSettings completionSettings, QuteFormattingSettings formattingSettings, CancelChecker cancelChecker) {
CompletionRequest completionRequest = null;
try {
completionRequest = new CompletionRequest(template, position, completionSettings, formattingSettings);
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Creation of CompletionRequest failed", e);
return EMPTY_FUTURE_COMPLETION;
}
Node node = completionRequest.getNode();
if (node == null) {
return EMPTY_FUTURE_COMPLETION;
}
String text = template.getText();
int offset = completionRequest.getOffset();
if (node.getKind() == NodeKind.Expression || node.getKind() == NodeKind.ExpressionParts || node.getKind() == NodeKind.ExpressionPart) {
Expression expression = null;
Node nodeExpression = null;
if (node.getKind() == NodeKind.Expression) {
expression = (Expression) node;
} else if (node.getKind() == NodeKind.ExpressionParts) {
nodeExpression = node;
expression = ((Parts) node).getParent();
} else if (node.getKind() == NodeKind.ExpressionPart) {
nodeExpression = node;
expression = ((Part) node).getParent().getParent();
}
return completionForExpression.doCompleteExpression(completionRequest, expression, nodeExpression, template, offset, completionSettings, formattingSettings, cancelChecker);
} else if (node.getKind() == NodeKind.Text) {
// The completion is triggered in text node (before node)
Section parent = node.getParentSection();
if (parent != null && (parent.isInEndTagName(offset))) {
// The completion is triggered inside end tag
return EMPTY_FUTURE_COMPLETION;
}
// The completion is triggered in text node
// Check if completion is triggered after a start bracket character and if it's
// a valid expression
int nbBrackets = 0;
int bracketOffset = offset - 1;
char previousChar = text.charAt(bracketOffset);
if (previousChar == '#') {
// {#
bracketOffset--;
}
while (bracketOffset >= 0 && text.charAt(bracketOffset) == '{') {
bracketOffset--;
nbBrackets++;
}
if (nbBrackets > 0) {
if (nbBrackets % 2 != 0) {
// The completion is triggered in text node after bracket '{' character
return completionForExpression.doCompleteExpression(completionRequest, null, node, template, offset, completionSettings, formattingSettings, cancelChecker);
}
return EMPTY_FUTURE_COMPLETION;
}
} else if (node.getKind() == NodeKind.ParameterDeclaration) {
return completionsForParameterDeclaration.doCollectJavaClassesSuggestions((ParameterDeclaration) node, template, offset, completionSettings);
} else if (node.getKind() == NodeKind.Section) {
// {#|}
return completionForTagSection.doCompleteTagSection(completionRequest, completionSettings, formattingSettings, cancelChecker);
}
return collectSnippetSuggestions(completionRequest);
}
use of com.redhat.qute.ls.commons.BadLocationException in project quarkus-ls by redhat-developer.
the class QuteCodeActions method doCodeActionsForUndefinedObject.
private static void doCodeActionsForUndefinedObject(Template template, Diagnostic diagnostic, QuteErrorCode errorCode, List<CodeAction> codeActions) {
try {
String varName = null;
boolean isIterable = false;
JsonObject data = (JsonObject) diagnostic.getData();
if (data != null) {
varName = data.get(DIAGNOSTIC_DATA_NAME).getAsString();
isIterable = data.get(DIAGNOSTIC_DATA_ITERABLE).getAsBoolean();
} else {
int offset = template.offsetAt(diagnostic.getRange().getStart());
Node node = template.findNodeAt(offset);
node = QutePositionUtility.findBestNode(offset, node);
if (node.getKind() == NodeKind.Expression) {
Expression expression = (Expression) node;
ObjectPart part = expression.getObjectPart();
if (part != null) {
varName = part.getPartName();
}
}
}
if (varName != null) {
TextDocument document = template.getTextDocument();
String lineDelimiter = document.lineDelimiter(0);
String title = MessageFormat.format(UNDEFINED_OBJECT_CODEACTION_TITLE, varName);
Position position = new Position(0, 0);
StringBuilder insertText = new StringBuilder("{@");
if (isIterable) {
insertText.append("java.util.List");
} else {
insertText.append("java.lang.String");
}
insertText.append(" ");
insertText.append(varName);
insertText.append("}");
insertText.append(lineDelimiter);
CodeAction insertParameterDeclarationQuickFix = CodeActionFactory.insert(title, position, insertText.toString(), document, diagnostic);
codeActions.add(insertParameterDeclarationQuickFix);
// CodeAction to set validation severity to ignore
doCodeActionToSetIgnoreSeverity(template, Collections.singletonList(diagnostic), errorCode, codeActions, UNDEFINED_OBJECT_SEVERITY_SETTING);
}
} catch (BadLocationException e) {
}
}
use of com.redhat.qute.ls.commons.BadLocationException in project quarkus-ls by redhat-developer.
the class QuteCompletionsForSnippets method collectSnippetSuggestions.
/**
* Collect snippets suggestions.
*
* @param completionRequest completion request.
* @param prefixFilter prefix filter.
* @param suffixToFind suffix to found to eat it when completion snippet is
* applied.
* @param list completion list to update.
*/
public void collectSnippetSuggestions(CompletionRequest completionRequest, String prefixFilter, String suffixToFind, CompletionList list) {
Node node = completionRequest.getNode();
int offset = completionRequest.getOffset();
Template template = node.getOwnerTemplate();
String text = template.getText();
int endExpr = offset;
// compute the from for search expression according to the node
int fromSearchExpr = getExprLimitStart(node, endExpr);
// compute the start expression
int startExpr = getExprStart(text, fromSearchExpr, endExpr);
try {
Range replaceRange = getReplaceRange(startExpr, endExpr, offset, template);
String lineDelimiter = template.lineDelimiter(replaceRange.getStart().getLine());
List<CompletionItem> snippets = getSnippetRegistry().getCompletionItems(replaceRange, lineDelimiter, completionRequest.canSupportMarkupKind(MarkupKind.MARKDOWN), completionRequest.isCompletionSnippetsSupported(), (context, model) -> {
if (context instanceof IQuteSnippetContext) {
return (((IQuteSnippetContext) context).isMatch(completionRequest, model));
}
return false;
}, (suffix) -> {
// Search the suffix from the right of completion offset.
for (int i = endExpr; i < text.length(); i++) {
char ch = text.charAt(i);
if (Character.isWhitespace(ch)) {
// whitespace, continue to eat character
continue;
} else {
// the current character is not a whitespace, search the suffix index
Integer eatIndex = getSuffixIndex(text, suffix, i);
if (eatIndex != null) {
try {
return template.positionAt(eatIndex);
} catch (BadLocationException e) {
return null;
}
}
return null;
}
}
return null;
}, suffixToFind, prefixFilter);
for (CompletionItem completionItem : snippets) {
list.getItems().add(completionItem);
}
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "In QuteCompletions, collectSnippetSuggestions position error", e);
}
}
use of com.redhat.qute.ls.commons.BadLocationException in project quarkus-ls by redhat-developer.
the class QuteReference method findReferences.
public List<? extends Location> findReferences(Template template, Position position, ReferenceContext context, CancelChecker cancelChecker) {
try {
int offset = template.offsetAt(position);
Node node = template.findNodeAt(offset);
if (node == null) {
return Collections.emptyList();
}
node = QutePositionUtility.findBestNode(offset, node);
List<Location> locations = new ArrayList<>();
//
QuteSearchUtils.searchReferencedObjects(//
node, //
offset, (n, range) -> {
Template targetDocument = n.getOwnerTemplate();
Range targetRange = QutePositionUtility.createRange(n.getStart(), n.getEnd(), targetDocument);
Location location = new Location(targetDocument.getUri(), targetRange);
locations.add(location);
}, false, cancelChecker);
return locations;
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "In QuteReference the client provided Position is at a BadLocation", e);
return Collections.emptyList();
}
}
use of com.redhat.qute.ls.commons.BadLocationException in project quarkus-ls by redhat-developer.
the class QuteDefinition method findDefinition.
public CompletableFuture<List<? extends LocationLink>> findDefinition(Template template, Position position, CancelChecker cancelChecker) {
try {
DefinitionRequest definitionRequest = new DefinitionRequest(template, position);
Node node = definitionRequest.getNode();
if (node == null) {
return NO_DEFINITION;
}
int offset = definitionRequest.getOffset();
switch(node.getKind()) {
case Section:
// - Java data model definition
return findDefinitionFromSection(offset, (Section) node, template, cancelChecker);
case ParameterDeclaration:
// Return Java class definition
return findDefinitionFromParameterDeclaration(offset, (ParameterDeclaration) node, template);
case Expression:
return findDefinitionFromExpression(offset, (Expression) node, template, cancelChecker);
case ExpressionPart:
Part part = (Part) node;
return findDefinitionFromPart(part, template, cancelChecker);
default:
// no definitions
return NO_DEFINITION;
}
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Failed creating DefinitionRequest", e);
return NO_DEFINITION;
}
}
Aggregations