Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for textDocument/rename #351

Merged
merged 8 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [0.1.4] Next release

### Added
- Support for LSP request `textDocument/rename` [#351](https://github.com/OCamlPro/superbol-studio-oss/pull/351)
nberth marked this conversation as resolved.
Show resolved Hide resolved
- Show documentation comments on hover information [#350](https://github.com/OCamlPro/superbol-studio-oss/pull/350)
- Completion for more grammar constructs [#322](https://github.com/OCamlPro/superbol-studio-oss/pull/322)
- Support for LSP request `textDocument/codeLens` [#349](https://github.com/OCamlPro/superbol-studio-oss/pull/349)
nberth marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
4 changes: 2 additions & 2 deletions src/lsp/cobol_lsp/lsp_capabilities.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ let reply (_: ClientCapabilities.t) =
in
ServerCapabilities.create_workspace ()
~workspaceFolders
in
let codeLensProvider = CodeLensOptions.create () in
and codeLensProvider = CodeLensOptions.create () in
ServerCapabilities.create ()
~textDocumentSync:(`TextDocumentSyncOptions sync)
~definitionProvider:(`Bool true)
Expand All @@ -61,3 +60,4 @@ let reply (_: ClientCapabilities.t) =
~workspace
~documentSymbolProvider:(`Bool true)
~codeLensProvider
~renameProvider:(`Bool true)
8 changes: 7 additions & 1 deletion src/lsp/cobol_lsp/lsp_imports.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@

module CUs = Cobol_unit.Collections.SET
module CUMap = Cobol_unit.Collections.MAP
module URIMap = Map.Make (Lsp.Uri)
module URIMap = struct
include Map.Make (Lsp.Uri)

let add_to_list x data m =
let add = function None -> Some [data] | Some l -> Some (data :: l) in
update x add m
end
40 changes: 37 additions & 3 deletions src/lsp/cobol_lsp/lsp_request.ml
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ let handle_folding_range registry (params: FoldingRangeParams.t) =

let handle_document_symbol registry (params: DocumentSymbolParams.t) =
try_with_main_document_data registry params.textDocument
~f:begin fun ~doc { ptree; _ }->
~f:begin fun ~doc { ptree; _ } ->
let uri = Lsp.Text_document.documentUri doc.textdoc in
let symbols = Lsp_document_symbol.from_ptree_at ~uri ptree in
Some (`DocumentSymbol symbols)
Expand Down Expand Up @@ -702,6 +702,38 @@ let handle_codelens registry ({ textDocument; _ }: CodeLensParams.t) =
end
|> Option.value ~default:[]

(** { Rename } *)

let handle_rename ?(ignore_when_copybook=false)
registry
({ textDocument; position; newName = newText; _ }: RenameParams.t) =
try_with_main_document_data registry textDocument
~f:begin fun ~doc checked_doc ->
let rootdir = Lsp_project.(string_of_rootdir @@ rootdir doc.project) in
let locations = Option.value ~default:[] @@
let context = ReferenceContext.create ~includeDeclaration:true in
let params = ReferenceParams.create
~context ~position ~textDocument () in
lookup_references_in_doc ~rootdir params checked_doc in
let changes, is_copybook =
List.fold_left begin fun (map, is_copybook) ({ range; uri }: Location.t) ->
URIMap.add_to_list uri (TextEdit.create ~newText ~range) map,
is_copybook || DocumentUri.compare uri textDocument.uri <> 0
end (URIMap.empty, false) locations in
let changes = List.of_seq @@ URIMap.to_seq changes in
if is_copybook && ignore_when_copybook
then begin Lsp_io.notify_info
"Ignored renaming of a reference that occurs in a copybook";
Some ( WorkspaceEdit.create () ) end
else
begin if is_copybook
then Lsp_io.notify_warn
"Proceeded to rename of a reference that occurs in a copybook";
Some ( WorkspaceEdit.create ~changes () ) end
end
|> Option.get


(** {3 Generic handling} *)

let shutdown: state -> unit = function
Expand Down Expand Up @@ -747,17 +779,18 @@ let on_request
Ok (handle_folding_range registry params, state)
| Shutdown ->
Ok (handle_shutdown registry, ShuttingDown)
| DocumentSymbol (* DocumentSymbolParams.t.t *) params ->
| DocumentSymbol params ->
Ok (handle_document_symbol registry params, state)
| TextDocumentCodeLens (* CodeLensParams.t.t *) params ->
Ok (handle_codelens registry params, state)
| TextDocumentRename params ->
Ok (handle_rename registry params, state)
| TextDocumentDeclaration (* TextDocumentPositionParams.t.t *) _
| TextDocumentTypeDefinition (* TypeDefinitionParams.t.t *) _
| TextDocumentImplementation (* ImplementationParams.t.t *) _
| TextDocumentCodeLensResolve (* CodeLens.t.t *) _
| TextDocumentPrepareCallHierarchy (* CallHierarchyPrepareParams.t.t *) _
| TextDocumentPrepareRename (* PrepareRenameParams.t.t *) _
| TextDocumentRename (* RenameParams.t.t *) _
| TextDocumentLink (* DocumentLinkParams.t.t *) _
| TextDocumentLinkResolve (* DocumentLink.t.t *) _
| TextDocumentMoniker (* MonikerParams.t.t *) _
Expand Down Expand Up @@ -825,4 +858,5 @@ module INTERNAL = struct
let codelens = handle_codelens
let document_symbol = handle_document_symbol
let formatting = handle_formatting
let rename = handle_rename
end
5 changes: 5 additions & 0 deletions src/lsp/cobol_lsp/lsp_request.mli
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ module INTERNAL: sig
: Lsp_server.t
-> Lsp.Types.CodeLensParams.t
-> Lsp.Types.CodeLens.t list
val rename
: ?ignore_when_copybook:bool
-> Lsp_server.t
-> Lsp.Types.RenameParams.t
-> Lsp.Types.WorkspaceEdit.t
end
6 changes: 6 additions & 0 deletions src/lsp/cobol_typeck/typeck_outputs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type references_in_unit =
data_refs: qualrefmap;
proc_refs: qualrefmap;
(* TODO: const_refs, prog_refs?, others... *)
(* TODO atm data_refs and proc_refs contains BOTH direct references and indirect references
e.g.: DISPLAY B IN A result in
- a direct reference to B
- a indirect reference to A
In the future we may need/want to split those 2 kind of references
*)
}
type references_in_group = references_in_unit Cobol_unit.Collections.MAP.t

Expand Down
24 changes: 17 additions & 7 deletions src/lsp/cobol_typeck/typeck_procedure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ let references ~(data_definitions: Cobol_unit.Types.data_definitions) procedure

method! fold_qualname qn acc = (* TODO: data_name' instead *)
let loc = baseloc_of_qualname qn in
Visitor.skip_children @@
Visitor.do_children @@
(* match Qualmap.find qn data_definitions.data_items.named with *)
(* | Data_field { def; _ } -> *)
(* { acc with *)
Expand Down Expand Up @@ -226,18 +226,28 @@ let references ~(data_definitions: Cobol_unit.Types.data_definitions) procedure
Cobol_ptree.Proc_division_visitor.fold_paragraph' v paragraph acc


method! fold_procedure_name' ({ loc; _ } as qn)
method! fold_procedure_name' qn
({ current_section = in_section; _ } as acc) =
Visitor.skip_children @@
match Cobol_unit.Procedure.find ~&qn ?in_section procedure with
| block ->
let register ?in_section qn acc =
let loc = baseloc_of_qualname ~&qn in
match Cobol_unit.Procedure.find ~&qn ?in_section procedure with
| block ->
{ acc with
refs = Typeck_outputs.register_procedure_ref ~loc block acc.refs }
| exception Not_found ->
| exception Not_found ->
error acc @@ Unknown_proc_name qn
| exception Qualmap.Ambiguous (lazy matching_qualnames) ->
| exception Qualmap.Ambiguous (lazy matching_qualnames) ->
error acc @@ Ambiguous_proc_name { given_qualname = qn;
matching_qualnames }
in
let acc = register ?in_section qn acc in
let acc = match ~&qn with
| Name _ -> acc
| Qual (_, section_qn) ->
let loc = baseloc_of_qualname section_qn in
register (section_qn &@ loc) acc
in
Visitor.skip_children acc

end in

Expand Down
11 changes: 6 additions & 5 deletions test/lsp/lsp_codelens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ let%expect_test "codelens" =
88 YYcond value "a".
PROCEDURE DIVISION.
MOVE aa TO aA.
DISPLAY BB IN AA.
STOP RUN.
|cobol} in
end_with_postproc [%expect.output];
Expand All @@ -60,7 +61,7 @@ let%expect_test "codelens" =
---- ^
7 02 BB PIC X.
8 02 BBprime REDEFINES BB PIC 9.
3 references
4 references
__rootdir__/prog.cob:7.13:
4 DATA DIVISION.
5 WORKING-STORAGE SECTION.
Expand All @@ -69,7 +70,7 @@ let%expect_test "codelens" =
---- ^
8 02 BBprime REDEFINES BB PIC 9.
9 02 CC PIC X. 02 DD PIC X.
3 references
4 references
__rootdir__/prog.cob:8.13:
5 WORKING-STORAGE SECTION.
6 01 AA.
Expand Down Expand Up @@ -154,7 +155,7 @@ let%expect_test "codelens-procedure" =
CC.
DD SECTION.
PERFORM AA.
PERFORM BB.
PERFORM BB IN AA.
GO DD.
STOP RUN.
|cobol} in
Expand All @@ -169,7 +170,7 @@ let%expect_test "codelens-procedure" =
---- ^
6 BB.
7 PERFORM BB.
2 references
3 references
__rootdir__/prog.cob:6.12:
3 PROGRAM-ID. prog.
4 PROCEDURE DIVISION.
Expand All @@ -195,5 +196,5 @@ let%expect_test "codelens-procedure" =
9 > DD SECTION.
---- ^
10 PERFORM AA.
11 PERFORM BB.
11 PERFORM BB IN AA.
2 references |}];;
4 changes: 2 additions & 2 deletions test/lsp/lsp_definition.ml
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,11 @@ let%expect_test "definition-index" =
SET _|3-i|_I IN V-TAB TO 0
SET _|4-j|_J IN w TO 0
SET _|5-k|_K IN W-TAB IN W TO 0
SET _|6-missing|_L IN w-tab IN W TO 0
SET _|6-missing|_L IN w-tab IN W TO 0.
|cobol};
end_with_postproc [%expect.output];
[%expect {|
{"params":{"diagnostics":[{"message":"Missing .","range":{"end":{"character":35,"line":11},"start":{"character":35,"line":11}},"severity":4}],"uri":"file://__rootdir__/prog.cob"},"method":"textDocument/publishDiagnostics","jsonrpc":"2.0"}
{"params":{"diagnostics":[],"uri":"file://__rootdir__/prog.cob"},"method":"textDocument/publishDiagnostics","jsonrpc":"2.0"}
1-j (line 4, character 41):
__rootdir__/prog.cob:5.41-5.42:
2 PROGRAM-ID. prog.
Expand Down
Loading
Loading