Skip to content

Commit

Permalink
Merge pull request #40891 from KavinduZoysa/fix-issue-39261
Browse files Browse the repository at this point in the history
Add completions for init function
  • Loading branch information
KavinduZoysa authored Jul 6, 2023
2 parents 3775e22 + 67b2092 commit 30a82a2
Show file tree
Hide file tree
Showing 26 changed files with 612 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import io.ballerina.compiler.api.symbols.Symbol;
import io.ballerina.compiler.syntax.tree.FunctionSignatureNode;
import io.ballerina.compiler.syntax.tree.FunctionTypeDescriptorNode;
import io.ballerina.compiler.syntax.tree.Minutiae;
import io.ballerina.compiler.syntax.tree.MinutiaeList;
import io.ballerina.compiler.syntax.tree.Node;
import io.ballerina.compiler.syntax.tree.NonTerminalNode;
import io.ballerina.compiler.syntax.tree.QualifiedNameReferenceNode;
Expand All @@ -27,13 +29,16 @@
import io.ballerina.tools.text.TextRange;
import org.ballerinalang.annotation.JavaSPIService;
import org.ballerinalang.langserver.common.utils.CommonUtil;
import org.ballerinalang.langserver.common.utils.PositionUtil;
import org.ballerinalang.langserver.commons.BallerinaCompletionContext;
import org.ballerinalang.langserver.commons.completion.LSCompletionItem;
import org.ballerinalang.langserver.completions.SnippetCompletionItem;
import org.ballerinalang.langserver.completions.providers.AbstractCompletionProvider;
import org.ballerinalang.langserver.completions.util.QNameRefCompletionUtil;
import org.ballerinalang.langserver.completions.util.Snippet;
import org.ballerinalang.langserver.completions.util.SortingUtil;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextEdit;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -76,12 +81,46 @@ public List<LSCompletionItem> getCompletions(BallerinaCompletionContext context,
ruleContext = RuleContext.PARAMETER_CTX;
} else if (this.withinReturnKWContext(context, node)) {
completionItems.add(new SnippetCompletionItem(context, Snippet.KW_RETURNS.get()));
} else if (node.parent().kind() == SyntaxKind.OBJECT_FIELD) {
/* Covers the completions for init function in object constructor or class definition
* eg: object { function <cursor> }
* object { public function <cursor> }
*/
SnippetCompletionItem initFuncCompletionItem =
new SnippetCompletionItem(context, Snippet.DEF_INIT_FUNCTION.get());
Token funcKW = node.functionKeyword();
int endOffset = getEndPosWithoutNewLine(funcKW);
Range range = PositionUtil.toRange(funcKW.textRange().startOffset(), endOffset,
context.currentDocument().get().textDocument());
TextEdit textEdit = new TextEdit(range, "");
initFuncCompletionItem.getCompletionItem().setAdditionalTextEdits(List.of(textEdit));
completionItems.add(initFuncCompletionItem);
completionItems.add(new SnippetCompletionItem(context, Snippet.DEF_FUNCTION.get()));
}
this.sort(context, node, completionItems, ruleContext);

return completionItems;
}

private int getEndPosWithoutNewLine(Token token) {
/* Purpose of this method to handle the following scenario specially
* eg: object { function<cursor> }
* If we have single trailing minutiae, and it is a new line, then we need to consider `function` keyword as
* an existing token.
*/
int end = token.textRangeWithMinutiae().endOffset();
MinutiaeList minutiaeList = token.trailingMinutiae();
int size = minutiaeList.size();
if (size == 0) {
return end;
}
Minutiae lastMinutiae = minutiaeList.get(size - 1);
if (lastMinutiae.kind() == SyntaxKind.END_OF_LINE_MINUTIAE) {
return size == 1 ? token.textRange().startOffset() : end - 1;
}
return end;
}

@Override
protected List<LSCompletionItem> getCompletionItemsOnQualifiers(Node node, BallerinaCompletionContext context) {
List<LSCompletionItem> completionItems = new ArrayList<>(super.getCompletionItemsOnQualifiers(node, context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
*/
package org.ballerinalang.langserver.completion;

import org.ballerinalang.langserver.commons.workspace.WorkspaceDocumentException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;

/**
* Function Definition Context tests.
Expand All @@ -31,6 +35,11 @@ public Object[][] dataProvider() {
return this.getConfigsList();
}

@Test(dataProvider = "completion-data-provider")
public void test(String config, String configPath) throws IOException, WorkspaceDocumentException {
super.test(config, configPath);
}

@Override
public String getTestResourceDir() {
return "function_def";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"position": {
"line": 2,
"character": 16
},
"source": "function_def/source/source23.bal",
"description": "init function completion in object constructor",
"items": [
{
"label": "init function",
"kind": "Snippet",
"detail": "Snippet",
"sortText": "P",
"filterText": "init_function",
"insertText": "function init(${1}) {\n\t${2}\n}",
"insertTextFormat": "Snippet",
"additionalTextEdits": [
{
"range": {
"start": {
"line": 2,
"character": 8
},
"end": {
"line": 2,
"character": 8
}
},
"newText": ""
}
]
},
{
"label": "function",
"kind": "Snippet",
"detail": "Snippet",
"sortText": "P",
"filterText": "function",
"insertText": "function ${1:name}(${2})${3} {\n\t${4}\n}",
"insertTextFormat": "Snippet"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"position": {
"line": 2,
"character": 18
},
"source": "function_def/source/source24.bal",
"description": "init function completion in object constructor",
"items": [
{
"label": "init function",
"kind": "Snippet",
"detail": "Snippet",
"sortText": "P",
"filterText": "init_function",
"insertText": "function init(${1}) {\n\t${2}\n}",
"insertTextFormat": "Snippet",
"additionalTextEdits": [
{
"range": {
"start": {
"line": 2,
"character": 8
},
"end": {
"line": 2,
"character": 17
}
},
"newText": ""
}
]
},
{
"label": "function",
"kind": "Snippet",
"detail": "Snippet",
"sortText": "P",
"filterText": "function",
"insertText": "function ${1:name}(${2})${3} {\n\t${4}\n}",
"insertTextFormat": "Snippet"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"position": {
"line": 2,
"character": 21
},
"source": "function_def/source/source25.bal",
"description": "init function completion in object constructor",
"items": [
{
"label": "init function",
"kind": "Snippet",
"detail": "Snippet",
"sortText": "P",
"filterText": "init_function",
"insertText": "function init(${1}) {\n\t${2}\n}",
"insertTextFormat": "Snippet",
"additionalTextEdits": [
{
"range": {
"start": {
"line": 2,
"character": 8
},
"end": {
"line": 2,
"character": 17
}
},
"newText": ""
}
]
},
{
"label": "function",
"kind": "Snippet",
"detail": "Snippet",
"sortText": "P",
"filterText": "function",
"insertText": "function ${1:name}(${2})${3} {\n\t${4}\n}",
"insertTextFormat": "Snippet"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"position": {
"line": 2,
"character": 23
},
"source": "function_def/source/source26.bal",
"description": "init function completion in object constructor",
"items": [
{
"label": "init function",
"kind": "Snippet",
"detail": "Snippet",
"sortText": "P",
"filterText": "init_function",
"insertText": "function init(${1}) {\n\t${2}\n}",
"insertTextFormat": "Snippet",
"additionalTextEdits": [
{
"range": {
"start": {
"line": 2,
"character": 15
},
"end": {
"line": 2,
"character": 15
}
},
"newText": ""
}
]
},
{
"label": "function",
"kind": "Snippet",
"detail": "Snippet",
"sortText": "P",
"filterText": "function",
"insertText": "function ${1:name}(${2})${3} {\n\t${4}\n}",
"insertTextFormat": "Snippet"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"position": {
"line": 2,
"character": 25
},
"source": "function_def/source/source27.bal",
"description": "init function completion in object constructor",
"items": [
{
"label": "init function",
"kind": "Snippet",
"detail": "Snippet",
"sortText": "P",
"filterText": "init_function",
"insertText": "function init(${1}) {\n\t${2}\n}",
"insertTextFormat": "Snippet",
"additionalTextEdits": [
{
"range": {
"start": {
"line": 2,
"character": 15
},
"end": {
"line": 2,
"character": 24
}
},
"newText": ""
}
]
},
{
"label": "function",
"kind": "Snippet",
"detail": "Snippet",
"sortText": "P",
"filterText": "function",
"insertText": "function ${1:name}(${2})${3} {\n\t${4}\n}",
"insertTextFormat": "Snippet"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"position": {
"line": 2,
"character": 28
},
"source": "function_def/source/source28.bal",
"description": "init function completion in object constructor",
"items": [
{
"label": "init function",
"kind": "Snippet",
"detail": "Snippet",
"sortText": "P",
"filterText": "init_function",
"insertText": "function init(${1}) {\n\t${2}\n}",
"insertTextFormat": "Snippet",
"additionalTextEdits": [
{
"range": {
"start": {
"line": 2,
"character": 15
},
"end": {
"line": 2,
"character": 24
}
},
"newText": ""
}
]
},
{
"label": "function",
"kind": "Snippet",
"detail": "Snippet",
"sortText": "P",
"filterText": "function",
"insertText": "function ${1:name}(${2})${3} {\n\t${4}\n}",
"insertTextFormat": "Snippet"
}
]
}
Loading

0 comments on commit 30a82a2

Please sign in to comment.