Skip to content

Amiralies fix file module refs #261

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
28 changes: 21 additions & 7 deletions analysis/src/References.ml
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,29 @@ 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.projectFiles
|> 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 targetModuleReferences =
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 ->
let moduleSrcToRef src = (Uri2.fromPath src, [Utils.topLoc src]) in
getSrc paths |> List.map moduleSrcToRef
in
locs
List.append targetModuleReferences otherModulesReferences
| Typed (_, _, NotFound) | LModule NotFound | Constant _ -> []
| TypeDefinition (_, _, stamp) -> forLocalStamp ~full stamp Type
| Typed (_, _, (LocalReference (stamp, tip) | Definition (stamp, tip)))
Expand Down
6 changes: 6 additions & 0 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion analysis/tests/src/expected/Cross.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}
]

32 changes: 25 additions & 7 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);