Fix als-other-file command for child packages

For this command, we were  calling Unit which expects an unit name
but we were passing a file base name (without extension).

So this doesn't work if the file base name contains '-'
(child packages).

Use the proper GPR2 API to get a proper unit name instead.

Add an automatic test.

For eng/ide/ada_language_server#1450
This commit is contained in:
Anthony Leonardo Gracio
2024-10-07 13:59:26 +00:00
parent 5b44580850
commit f286899f01
7 changed files with 199 additions and 11 deletions

View File

@@ -15,6 +15,8 @@
-- of the license. --
------------------------------------------------------------------------------
with GPR2.Build.Unit_Info;
with GPR2.Project.View;
with VSS.JSON.Streams;
with GNATCOLL.VFS; use GNATCOLL.VFS;
@@ -126,20 +128,27 @@ package body LSP.Ada_Handlers.Other_File_Commands is
-- Unit_For_File --
-------------------
function Unit_For_File return GPR2.Build.Compilation_Unit.Object
is
Unit : GPR2.Build.Compilation_Unit.Object;
function Unit_For_File return GPR2.Build.Compilation_Unit.Object is
begin
-- First look in the closure of sources, then in the
-- runtime project.
-- Check in the root project's closure for a visible source
-- corresponding to this file.
-- Not that the root's project closure includes the runtime.
if Handler.Project_Tree.Is_Defined then
Unit := Handler.Project_Tree.Root_Project.Unit (F.Base_Name);
declare
View : constant GPR2.Project.View.Object :=
Handler.Project_Tree.Root_Project;
Unit_Info : constant GPR2.Build.Unit_Info.Object :=
View.Visible_Source (F.Simple_Name).Unit;
Unit : GPR2.Build.Compilation_Unit.Object :=
GPR2.Build.Compilation_Unit.Undefined;
begin
if Unit_Info.Is_Defined then
Unit := View.Namespace_Roots.First_Element.Unit
(Unit_Info.Name);
end if;
if not Unit.Is_Defined then
Unit := Handler.Project_Tree.Runtime_Project.Unit
(F.Base_Name);
end if;
return Unit;
return Unit;
end;
else
return GPR2.Build.Compilation_Unit.Undefined;
end if;

View File

@@ -0,0 +1,8 @@
package body A.B is
procedure Proc (X : in out Integer) is
begin
X := X + 2 * X;
Proc (X => X);
end Proc;
end A.B;

View File

@@ -0,0 +1,3 @@
package A.B is
procedure Proc (X : in out Integer);
end A.B;

View File

@@ -0,0 +1,2 @@
package A with Pure is
end A;

View File

@@ -0,0 +1,2 @@
project Default is
end Default;

View File

@@ -0,0 +1,163 @@
[
{
"comment": "Check that the 'als-other-file' command works fine with child packages"
},
{
"start": {
"cmd": ["${ALS}"]
}
},
{
"send": {
"request": {
"jsonrpc": "2.0",
"id": 0,
"method": "initialize",
"params": {
"processId": 1,
"rootUri": "$URI{.}",
"capabilities": {}
}
},
"wait": [
{
"id": 0,
"result": {
"capabilities": {
"textDocumentSync": 2,
"executeCommandProvider": {
"commands": ["<HAS>", "als-other-file"]
}
}
}
}
]
}
},
{
"send": {
"request": {
"jsonrpc": "2.0",
"method": "workspace/didChangeConfiguration",
"params": {
"settings": {
"ada": {}
}
}
},
"wait": [
{
"jsonrpc": "2.0",
"id": 1,
"method": "window/workDoneProgress/create",
"params": {
"token": "<ANY>"
}
}
]
}
},
{
"send": {
"request": {
"jsonrpc": "2.0",
"method": "textDocument/didOpen",
"params": {
"textDocument": {
"uri": "$URI{a-b.ads}",
"languageId": "ada",
"version": 1,
"text": "package A.B is\n procedure Proc (X : in out Integer);\nend A.B;\n"
}
}
},
"wait": []
}
},
{
"send": {
"request": {
"jsonrpc": "2.0",
"id": "sw1",
"method": "workspace/executeCommand",
"params": {
"command": "als-other-file",
"arguments": [
{
"uri": "$URI{a-b.ads}"
}
]
}
},
"wait": [
{
"jsonrpc": "2.0",
"method": "window/showDocument",
"params": {
"uri": "$URI{a-b.adb}",
"takeFocus": true
}
},
{
"jsonrpc": "2.0",
"id": "sw1",
"result": null
}
]
}
},
{
"send": {
"request": {
"jsonrpc": "2.0",
"id": "sw2",
"method": "workspace/executeCommand",
"params": {
"command": "als-other-file",
"arguments": [
{
"uri": "$URI{a-b.adb}"
}
]
}
},
"wait": [
{
"jsonrpc": "2.0",
"method": "window/showDocument",
"params": {
"uri": "$URI{a-b.ads}",
"takeFocus": true
}
},
{
"jsonrpc": "2.0",
"id": "sw2",
"result": null
}
]
}
},
{
"send": {
"request": {
"jsonrpc": "2.0",
"id": "shutdown",
"method": "shutdown",
"params": null
},
"wait": [{ "id": "shutdown", "result": null }]
}
},
{
"send": {
"request": { "jsonrpc": "2.0", "method": "exit" },
"wait": []
}
},
{
"stop": {
"exit_code": 0
}
}
]

View File

@@ -0,0 +1 @@
title: 'commands.other_file'