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

feat(lsp): Allow goto definition on include statements #2027

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions compiler/src/language_server/definition.re
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ let process =
| [Type({definition}), ..._]
| [Declaration({definition}), ..._]
| [Exception({definition}), ..._]
| [Module({definition}), ..._] =>
| [Module({definition}), ..._]
| [Include({definition}), ..._] =>
switch (definition) {
| None => send_no_result(~id)
| Some(loc) =>
let uri = Utils.filename_to_uri(loc.loc_start.pos_fname);

send_definition(
~id,
~range=Utils.loc_to_range(loc),
Expand Down
8 changes: 7 additions & 1 deletion compiler/src/language_server/sourcetree.re
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ module type Sourcetree = {
| Include({
path: Path.t,
loc: Location.t,
definition: option(Location.t),
});

type sourcetree = t(node);
Expand Down Expand Up @@ -261,6 +262,7 @@ module Sourcetree: Sourcetree = {
| Include({
path: Path.t,
loc: Location.t,
definition: option(Location.t),
});

type sourcetree = t(node);
Expand Down Expand Up @@ -532,7 +534,11 @@ module Sourcetree: Sourcetree = {
[
(
loc_to_interval(stmt.ttop_loc),
Include({path: inc.tinc_path, loc: stmt.ttop_loc}),
Include({
path: inc.tinc_path,
loc: stmt.ttop_loc,
definition: Some(inc.tinc_src),
}),
),
...segments^,
]
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/parsing/location.re
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ type t =
};

let in_file = name => {
let loc = {pos_fname: name, pos_lnum: 1, pos_bol: 0, pos_cnum: (-1)};
let loc = {pos_fname: name, pos_lnum: 1, pos_bol: 0, pos_cnum: 0};
{loc_start: loc, loc_end: loc, loc_ghost: true};
};

Expand Down
3 changes: 2 additions & 1 deletion compiler/src/typed/env.re
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,8 @@ let find_pers_struct = (~loc, check, filepath) => {
};

let load_pers_struct = (~loc, filepath) => {
find_pers_struct(~loc, false, filepath).ps_name;
let {ps_name, ps_filename} = find_pers_struct(~loc, false, filepath);
(ps_name, ps_filename);
};

/* Emits a warning if there is no valid cmi for name */
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/typed/env.rei
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ let normalize_path_prefix: (option(Location.t), t, Path.t) => Path.t;

let has_local_constraints: t => bool;

let load_pers_struct: (~loc: Location.t, string) => string;
let load_pers_struct: (~loc: Location.t, string) => (string, string);

/* By-identifier lookups */
/** Looks up the value associated with the given identifier. */
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/typed/typedtree.re
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ and match_branch = {
type include_declaration = {
tinc_path: Path.t,
[@sexp_drop_if sexp_locs_disabled]
tinc_src: Location.t,
[@sexp_drop_if sexp_locs_disabled]
tinc_loc: Location.t,
};

Expand Down
1 change: 1 addition & 0 deletions compiler/src/typed/typedtree.rei
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ and match_branch = {
[@deriving sexp]
type include_declaration = {
tinc_path: Path.t,
tinc_src: Location.t,
tinc_loc: Location.t,
};

Expand Down
13 changes: 11 additions & 2 deletions compiler/src/typed/typemod.re
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ let extract_sig_open = (env, loc, mty) =>

let include_module = (env, sod) => {
let include_path = sod.pinc_path.txt;
let mod_name = Env.load_pers_struct(~loc=sod.pinc_loc, include_path);
let (mod_name, mod_file) =
Env.load_pers_struct(~loc=sod.pinc_loc, include_path);
if (mod_name != sod.pinc_module.txt) {
raise(
Error(
Expand All @@ -104,7 +105,15 @@ let include_module = (env, sod) => {
);
let newenv = Env.include_module(mod_name, sod, env);

let od = {tinc_path: path, tinc_loc: sod.pinc_loc};
let mod_file =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one question before I press approve. When would the module file end in .wasm ?

if (String.ends_with(~suffix=".wasm", mod_file)) {
String.sub(mod_file, 0, String.length(mod_file) - 5);
} else {
mod_file;
};
let tinc_src = Location.in_file(mod_file);

let od = {tinc_path: path, tinc_src, tinc_loc: sod.pinc_loc};

(path, newenv, od);
};
Expand Down
Loading