From 6ddf611fcfba74213329cdcb4d7a3144f8c1b069 Mon Sep 17 00:00:00 2001 From: Amirali Esmaeili Date: Wed, 26 May 2021 05:33:43 +0430 Subject: [PATCH 1/5] Fix references to file modules --- analysis/src/References.ml | 29 +++++++++++++++++------ analysis/tests/src/expected/Cross.res.txt | 3 ++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/analysis/src/References.ml b/analysis/src/References.ml index 3da82433e..b313b9044 100644 --- a/analysis/src/References.ml +++ b/analysis/src/References.ml @@ -459,15 +459,30 @@ let forLocalStamp ~full:{file; extra; package} stamp tip = let allReferencesForLocItem ~full:({file; package} as full) locItem = match locItem.locType with | TopLevelModule moduleName -> - let locs = - match Hashtbl.find_opt full.extra.fileReferences moduleName with + let otherModulesReferences = + package.localModules + |> Utils.filterMap (fun name -> + match ProcessCmt.fileForModule ~package name with + | None -> None + | Some file -> ProcessCmt.getFullFromCmt ~uri:file.uri) + |> List.map (fun full -> + match Hashtbl.find_opt full.extra.fileReferences moduleName with + | None -> [] + | Some locs -> + locs + |> List.map (fun loc -> + (Uri2.fromPath loc.Location.loc_start.pos_fname, [loc]))) + |> List.flatten + in + let targetModuleReference = + match Hashtbl.find_opt package.pathsForModule moduleName with | None -> [] - | Some locs -> - locs - |> List.map (fun loc -> - (Uri2.fromPath loc.Location.loc_start.pos_fname, [loc])) + | Some paths -> ( + match SharedTypes.getSrc paths with + | None -> [] + | Some src -> [(Uri2.fromPath src, [Utils.topLoc src])]) in - locs + List.append targetModuleReference otherModulesReferences | Typed (_, _, NotFound) | LModule NotFound | Constant _ -> [] | TypeDefinition (_, _, stamp) -> forLocalStamp ~full stamp Type | Typed (_, _, (LocalReference (stamp, tip) | Definition (stamp, tip))) diff --git a/analysis/tests/src/expected/Cross.res.txt b/analysis/tests/src/expected/Cross.res.txt index 63f400742..fbce959f9 100644 --- a/analysis/tests/src/expected/Cross.res.txt +++ b/analysis/tests/src/expected/Cross.res.txt @@ -3,6 +3,7 @@ References tests/src/Cross.res 0:17 {"uri": "Cross.res", "range": {"start": {"line": 0, "character": 15}, "end": {"line": 0, "character": 25}}}, {"uri": "Cross.res", "range": {"start": {"line": 3, "character": 16}, "end": {"line": 3, "character": 26}}}, {"uri": "Cross.res", "range": {"start": {"line": 6, "character": 13}, "end": {"line": 6, "character": 23}}}, -{"uri": "Cross.res", "range": {"start": {"line": 8, "character": 16}, "end": {"line": 8, "character": 26}}} +{"uri": "Cross.res", "range": {"start": {"line": 8, "character": 16}, "end": {"line": 8, "character": 26}}}, +{"uri": "References.res", "range": {"start": {"line": 0, "character": 0}, "end": {"line": 0, "character": 0}}} ] From a328945474fb13786ff7d979dc375a4f98a68cb2 Mon Sep 17 00:00:00 2001 From: Amirali Esmaeili Date: Wed, 26 May 2021 17:26:53 +0430 Subject: [PATCH 2/5] Fix reference to interfaces --- analysis/src/References.ml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/analysis/src/References.ml b/analysis/src/References.ml index b313b9044..45ee775ff 100644 --- a/analysis/src/References.ml +++ b/analysis/src/References.ml @@ -474,15 +474,19 @@ let allReferencesForLocItem ~full:({file; package} as full) locItem = (Uri2.fromPath loc.Location.loc_start.pos_fname, [loc]))) |> List.flatten in - let targetModuleReference = + let targetModuleReferences = match Hashtbl.find_opt package.pathsForModule moduleName with | None -> [] | Some paths -> ( - match SharedTypes.getSrc paths with - | None -> [] - | Some src -> [(Uri2.fromPath src, [Utils.topLoc src])]) + let moduleSrcToRef src = (Uri2.fromPath src, [Utils.topLoc src]) in + match paths with + | Impl (_, None) -> [] + | Impl (_, Some src) -> [moduleSrcToRef src] + | Intf (_, srci) -> [moduleSrcToRef srci] + | IntfAndImpl (_, srci, _, src) -> + [moduleSrcToRef srci; moduleSrcToRef src]) in - List.append targetModuleReference otherModulesReferences + List.append targetModuleReferences otherModulesReferences | Typed (_, _, NotFound) | LModule NotFound | Constant _ -> [] | TypeDefinition (_, _, stamp) -> forLocalStamp ~full stamp Type | Typed (_, _, (LocalReference (stamp, tip) | Definition (stamp, tip))) From f15a8380e807df8e2efd7e11255ab2ecbf264ae3 Mon Sep 17 00:00:00 2001 From: Amirali Esmaeili Date: Wed, 26 May 2021 23:57:53 +0430 Subject: [PATCH 3/5] Add support for file renames --- server/src/server.ts | 32 +++++++++++++++++++++++++------- server/src/utils.ts | 8 ++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/server/src/server.ts b/server/src/server.ts index d6e72584b..81330412f 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -15,7 +15,7 @@ import * as utils from "./utils"; import * as c from "./constants"; import * as chokidar from "chokidar"; import { assert } from "console"; -import { fileURLToPath } from "url"; +import { fileURLToPath, pathToFileURL } from "url"; import { ChildProcess } from "child_process"; import { WorkspaceEdit } from "vscode-languageserver"; import { TextEdit } from "vscode-languageserver-types"; @@ -294,16 +294,34 @@ function rename(msg: p.RequestMessage) { if (locations === null) { result = null; } else { - let changes: { [uri: string]: TextEdit[] } = {}; + let textEdits: { [uri: string]: TextEdit[] } = {}; + let documentChanges: (p.RenameFile | p.TextDocumentEdit)[] = []; + locations.forEach(({ uri, range }) => { - let textEdit: TextEdit = { range, newText: params.newName }; - if (uri in changes) { - changes[uri].push(textEdit); + if (utils.isRangeTopOfFile(range)) { + let filePath = fileURLToPath(uri); + let newFilePath = `${path.dirname(filePath)}/${params.newName}${path.extname(filePath)}`; + let newUri = pathToFileURL(newFilePath).href; + let rename: p.RenameFile = { kind: "rename", oldUri: uri, newUri }; + documentChanges.push(rename); } else { - changes[uri] = [textEdit]; + let textEdit: TextEdit = { range, newText: params.newName }; + if (uri in textEdits) { + textEdits[uri].push(textEdit); + } else { + textEdits[uri] = [textEdit]; + } } }); - result = { changes }; + + Object.entries(textEdits) + .forEach(([uri, edits]) => { + let textDocumentEdit = { textDocument: { uri, version: null }, edits }; + documentChanges.push(textDocumentEdit); + }); + + + result = { documentChanges }; } let response: m.ResponseMessage = { jsonrpc: c.jsonrpcVersion, diff --git a/server/src/utils.ts b/server/src/utils.ts index 2c6d236f9..b32fd80a6 100644 --- a/server/src/utils.ts +++ b/server/src/utils.ts @@ -476,3 +476,11 @@ export let parseCompilerLogOutput = ( return { done, result }; }; + +export let isRangeTopOfFile = (range: p.Range) => + [ + range.start.character, + range.start.line, + range.end.character, + range.end.line + ].every(n => n === 0); From 13a09b39ba3f6b4b0d4eb8a0c011599caac97903 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 30 May 2021 04:54:34 +0200 Subject: [PATCH 4/5] Fix build after rebase. --- analysis/src/References.ml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/analysis/src/References.ml b/analysis/src/References.ml index 45ee775ff..348f788eb 100644 --- a/analysis/src/References.ml +++ b/analysis/src/References.ml @@ -460,7 +460,7 @@ let allReferencesForLocItem ~full:({file; package} as full) locItem = match locItem.locType with | TopLevelModule moduleName -> let otherModulesReferences = - package.localModules + package.projectFiles |> Utils.filterMap (fun name -> match ProcessCmt.fileForModule ~package name with | None -> None @@ -480,11 +480,9 @@ let allReferencesForLocItem ~full:({file; package} as full) locItem = | Some paths -> ( let moduleSrcToRef src = (Uri2.fromPath src, [Utils.topLoc src]) in match paths with - | Impl (_, None) -> [] - | Impl (_, Some src) -> [moduleSrcToRef src] - | Intf (_, srci) -> [moduleSrcToRef srci] - | IntfAndImpl (_, srci, _, src) -> - [moduleSrcToRef srci; moduleSrcToRef src]) + | Impl {res} -> [moduleSrcToRef res] + | IntfAndImpl {resi; res} -> [moduleSrcToRef resi; moduleSrcToRef res] + | Namespace _ -> []) in List.append targetModuleReferences otherModulesReferences | Typed (_, _, NotFound) | LModule NotFound | Constant _ -> [] From f8802ba91d8071eb7e31301cbe0a0e39b536edd6 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 30 May 2021 04:59:23 +0200 Subject: [PATCH 5/5] Refactor getSrc --- analysis/src/References.ml | 7 ++----- analysis/src/SharedTypes.ml | 6 ++++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/analysis/src/References.ml b/analysis/src/References.ml index 348f788eb..43095c8a0 100644 --- a/analysis/src/References.ml +++ b/analysis/src/References.ml @@ -477,12 +477,9 @@ let allReferencesForLocItem ~full:({file; package} as full) locItem = let targetModuleReferences = match Hashtbl.find_opt package.pathsForModule moduleName with | None -> [] - | Some paths -> ( + | Some paths -> let moduleSrcToRef src = (Uri2.fromPath src, [Utils.topLoc src]) in - match paths with - | Impl {res} -> [moduleSrcToRef res] - | IntfAndImpl {resi; res} -> [moduleSrcToRef resi; moduleSrcToRef res] - | Namespace _ -> []) + getSrc paths |> List.map moduleSrcToRef in List.append targetModuleReferences otherModulesReferences | Typed (_, _, NotFound) | LModule NotFound | Constant _ -> [] diff --git a/analysis/src/SharedTypes.ml b/analysis/src/SharedTypes.ml index ca5ebeddf..948996527 100644 --- a/analysis/src/SharedTypes.ml +++ b/analysis/src/SharedTypes.ml @@ -130,6 +130,12 @@ let showPaths paths = | IntfAndImpl {cmti; resi; cmt; res} -> Printf.sprintf "IntfAndImpl(%s, %s, %s, %s)" cmti resi cmt res +let getSrc p = + match p with + | Impl {res} -> [res] + | Namespace _ -> [] + | IntfAndImpl {resi; res} -> [resi; res] + let getUri p = match p with | Impl {res} -> Uri2.fromPath res