Skip to content

Commit

Permalink
Merge pull request #41256 from KavinduZoysa/fix-issue-34801
Browse files Browse the repository at this point in the history
Make goto-def and renaming work for table and stream row type
  • Loading branch information
KavinduZoysa authored Oct 19, 2023
2 parents 786f8ac + 59d0ae4 commit ac3d86c
Show file tree
Hide file tree
Showing 20 changed files with 425 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.symbols.Symbol;
import io.ballerina.compiler.syntax.tree.ModulePartNode;
import io.ballerina.compiler.syntax.tree.NonTerminalNode;
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.projects.Document;
import io.ballerina.projects.Module;
import io.ballerina.projects.Project;
import io.ballerina.tools.diagnostics.Location;
import io.ballerina.tools.text.LinePosition;
import io.ballerina.tools.text.TextRange;
import org.ballerinalang.langserver.common.utils.CommonUtil;
import org.ballerinalang.langserver.common.utils.PathUtil;
import org.ballerinalang.langserver.common.utils.PositionUtil;
Expand Down Expand Up @@ -120,7 +122,22 @@ public static Optional<Symbol> getSymbolAtCursor(PositionedOperationContext cont
return Optional.empty();
}

Document document = srcFile.get();
Position position = context.getCursorPosition();
TextRange range = TextRange.from(
document.textDocument().textPositionFrom(PositionUtil.getLinePosition(position)), 0);
NonTerminalNode nonTerminalNode = ((ModulePartNode) document.syntaxTree().rootNode()).findNode(range);
SyntaxKind parentKind = nonTerminalNode.parent().kind();

if (parentKind == SyntaxKind.TYPE_PARAMETER || parentKind == SyntaxKind.STREAM_TYPE_PARAMS) {
if (nonTerminalNode.lineRange().endLine().offset() == position.getCharacter()) {
// When there is a type parameter and cursor is at the end of the type parameter, semantic API does not
// provide the correct symbol. Therefore, here we search for the symbol at (col - 1).
return semanticModel.get().symbol(document,
LinePosition.from(position.getLine(), position.getCharacter() - 1));
}
}

Optional<Symbol> symbolAtCursor = semanticModel.get().symbol(srcFile.get(),
LinePosition.from(position.getLine(), position.getCharacter()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ protected Object[][] testDataProvider() throws IOException {
{"def_annotation_on_obj_func_config1.json", "project"},
{"def_typereference.json", "project"},
{"def_typereference2.json", "project"},
{"def_typereference3.json", "project"}
{"def_typereference3.json", "project"},
{"defProject15.json", "project"},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private Object[][] testDataProvider() {
// Negative/invalid cases
{"rename_keyword_result1.json", "kw"},
{"rename_invalid_token_result1.json", "token"},
{"rename_table_row_type_and_stream_type.json", "Student"},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ public Object[][] testDataProvider() {
{"rename_self.json", "this"},
{"rename_invalid_qname_ref.json", "io"},
// {"rename_resource_method_path_segment.json", "path1"} //TODO: Fix #41041

{"rename_table_row_type_parameter1.json", "Student"},
{"rename_table_row_type_parameter2.json", "Student"},
{"rename_table_row_type_parameter3.json", "Student"},
{"rename_stream_type_parameter.json", "Student"},
{"rename_map_type_parameter.json", "Student"},
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"source": {
"file": "projectls/defmodsource6.bal"
},
"position": {
"line": 3,
"character": 19
},
"result": [
{
"range": {
"start": {
"line": 9,
"character": 12
},
"end": {
"line": 9,
"character": 17
}
},
"uri": "projectls/modules/lsmod3/lsmod3.bal"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import projectls.lsmod3;

function foo() {
table<lsmod3:Human> tHuman = table [{id: 1, name: "Jane"}];
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ public function mod3Function1() {

public type MyType lsmod1:MyType;
public type MyType2 MyType;

public type Human record {|
readonly int id;
string name;
|};
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"source": {
"file": "main.bal"
},
"position": {
"line": 19,
"character": 23
},
"prepareRename": {
"valid": true
},
"result": {
"changes": {
"modules/module2/types.bal": [{
"range": {
"start": {
"line": 2,
"character": 12
},
"end": {
"line": 2,
"character": 17
}
},
"newText": "Student"
}],
"main.bal": [{
"range": {
"start": {
"line": 18,
"character": 19
},
"end": {
"line": 18,
"character": 24
}
},
"newText": "Student"
}, {
"range": {
"start": {
"line": 19,
"character": 18
},
"end": {
"line": 19,
"character": 23
}
},
"newText": "Student"
}]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"source": {
"file": "rename_map_type_parameter.bal"
},
"position": {
"line": 6,
"character": 14
},
"prepareRename": {
"valid": true
},
"result": {
"changes": {
"rename_map_type_parameter.bal": [
{
"range": {
"start": {
"line": 0,
"character": 5
},
"end": {
"line": 0,
"character": 11
}
},
"newText": "Student"
},
{
"range": {
"start": {
"line": 6,
"character": 8
},
"end": {
"line": 6,
"character": 14
}
},
"newText": "Student"
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"source": {
"file": "rename_stream_type_parameter.bal"
},
"position": {
"line": 7,
"character": 17
},
"prepareRename": {
"valid": true
},
"result": {
"changes": {
"rename_stream_type_parameter.bal": [
{
"range": {
"start": {
"line": 0,
"character": 5
},
"end": {
"line": 0,
"character": 11
}
},
"newText": "Student"
},
{
"range": {
"start": {
"line": 6,
"character": 4
},
"end": {
"line": 6,
"character": 10
}
},
"newText": "Student"
},
{
"range": {
"start": {
"line": 7,
"character": 11
},
"end": {
"line": 7,
"character": 17
}
},
"newText": "Student"
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"source": {
"file": "rename_table_row_type_parameter1.bal"
},
"position": {
"line": 6,
"character": 16
},
"prepareRename": {
"valid": true
},
"result": {
"changes": {
"rename_table_row_type_parameter1.bal": [
{
"range": {
"start": {
"line": 0,
"character": 5
},
"end": {
"line": 0,
"character": 11
}
},
"newText": "Student"
},
{
"range": {
"start": {
"line": 6,
"character": 10
},
"end": {
"line": 6,
"character": 16
}
},
"newText": "Student"
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"source": {
"file": "rename_table_row_type_parameter2.bal"
},
"position": {
"line": 7,
"character": 16
},
"prepareRename": {
"valid": true
},
"result": {
"changes": {
"rename_table_row_type_parameter2.bal": [
{
"range": {
"start": {
"line": 0,
"character": 5
},
"end": {
"line": 0,
"character": 11
}
},
"newText": "Student"
},
{
"range": {
"start": {
"line": 6,
"character": 4
},
"end": {
"line": 6,
"character": 10
}
},
"newText": "Student"
},
{
"range": {
"start": {
"line": 7,
"character": 10
},
"end": {
"line": 7,
"character": 16
}
},
"newText": "Student"
},
{
"range": {
"start": {
"line": 8,
"character": 11
},
"end": {
"line": 8,
"character": 17
}
},
"newText": "Student"
}
]
}
}
}
Loading

0 comments on commit ac3d86c

Please sign in to comment.