Skip to content

Fix references to file modules #254

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

Merged
merged 11 commits into from
Jun 5, 2021

Conversation

amiralies
Copy link
Contributor

No description provided.

@amiralies
Copy link
Contributor Author

This pr adds support for global references to a module.
if you have these structure.

// A.res
let a = 2
// B.res
let x = A.a
// C.res
let x = A.a

requesting refrence on A in either B or C results in references to all these three files.
note that reference to A itself is a special (0,0), (0,0) loc.

@cristianoc
Copy link
Collaborator

See how that interacts with rename:

Screenshot 2021-05-26 at 04 10 59

@cristianoc
Copy link
Collaborator

There's also this confusing thing that the first line

let x = 12

is considered as a reference to References.res.
That would leave me puzzled as a user.

So basically 2 things to be solved in user experience.

@amiralies
Copy link
Contributor Author

See how that interacts with rename:

Will fix that

is considered as a reference to References.res.

Do you think that's bad experience? for me it's like referencing to let binding when i get references on the usage site
for example

let x = 2

let b = x

if i get refrence on second x, it includes the first let binding which declares x.

@cristianoc
Copy link
Collaborator

This module reference is different. We want a reference to a module, we get the first line of a file. The user will look at the first line, and might think: how did I get here? That first line has nothing to do with the original query, except it happens to be in the file that the query is about.
It's not a terrible experience, but not a great experience either. That said, I'm not sure it's possible to do better.

@amiralies
Copy link
Contributor Author

Yeah your argument is valid.
and it's happening becaus module decleration for file-modules is implicit, we don't have a exact location to refer to that so we refer to top of file.

@amiralies
Copy link
Contributor Author

File rename demo

recording.mp4

changes[uri].push(textEdit);
if (utils.isRangeTopOfFile(range)) {
let filePath = fileURLToPath(uri);
let newFilePath = `${path.dirname(filePath)}/${params.newName}${path.extname(filePath)}`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

what about Windows paths?

| Some paths -> (
let moduleSrcToRef src = (Uri2.fromPath src, [Utils.topLoc src]) in
match paths with
| Impl (_, None) -> []
Copy link
Collaborator

@cristianoc cristianoc May 26, 2021

Choose a reason for hiding this comment

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

How about move this extraction of source files to SharedTypes alongside getSrc.
So that it returns a list of source files.

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

This is great, thanks a lot.
I've added a few comments.

In addition, how about adapting the test files so they cover all cases, including implementation and interface files, as well as references from another file than the current one.

@amiralies
Copy link
Contributor Author

thank you i will address them.
I've a question, should we move rename stuff into analysis cli in order to test them separately or testing references is enough?

@cristianoc
Copy link
Collaborator

I think eventually it will have to move, as e.g. the isRangeTopOfFile check really belongs there, where one can make it less indirect.

@amiralies
Copy link
Contributor Author

I will move if it's okay.

another thing have to be fixed. looks like there's a problem when the project has namespace: true in bsconfig.

@cristianoc
Copy link
Collaborator

Great go ahead with moving.

@cristianoc
Copy link
Collaborator

@amiralies meanwhile I've done some refactoring relevant to a couple of lines of this diff:
#256

Specifically, type paths in SharedTypes.ml.

I can merge and this gets rebased, or it can be done later.

@amiralies
Copy link
Contributor Author

Maybe you can merge your branch then I will update my branch/PR

@cristianoc
Copy link
Collaborator

Merged

@cristianoc
Copy link
Collaborator

@amiralies a couple of next steps that fix build and clean up after rebase:
#261

Feel free to incorporate and continue. Or I'm also happy to continue if you prefer.

@amiralies
Copy link
Contributor Author

I will merge your branch on mine, and continue the work here if it's okay.

@cristianoc
Copy link
Collaborator

Perfect

@cristianoc
Copy link
Collaborator

Windows tests are failing, I think because the order in which files are processed is nondeterministic. And can become platform-dependent.

match References.locItemForPos ~full pos with
| None -> Protocol.null
| Some locItem ->
let allReferences = References.allReferencesForLocItem ~full locItem in
Copy link
Collaborator

Choose a reason for hiding this comment

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

This list I think can be non-deterministic.
Some fixed order should be chosen (e.g. by sorting the results on Uri then loc).

Copy link
Collaborator

@cristianoc cristianoc Jun 4, 2021

Choose a reason for hiding this comment

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

It might be enough to make fileReferences inside extra in SharedTypes.ml store a set of locations instead of a list.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will check what's going wrong on a windows setup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like there was an issue checking for interface file on windows

Copy link
Collaborator

Choose a reason for hiding this comment

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

Great thanks. I've also looked at removing some nondeterminism:
#263

Which I might add later on. To make debugging across platforms easier.

@amiralies
Copy link
Contributor Author

Works to be done aside from issue on windows:

  • Rename tests
  • Use rename command in server

@amiralies
Copy link
Contributor Author

I think it's ready for review !

let dir = Filename.dirname path in
let ext = Filename.extension path in
let sep = Filename.dir_sep in
let newPath = dir ^ sep ^ newName ^ ext in
Copy link
Collaborator

Choose a reason for hiding this comment

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

Better use Filename.concat instead of string concat with sep.

let newUri = Uri2.fromPath newPath in
Protocol.
{
kind = `rename;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I think it's easier to expose a constructor function in the protocol that only takes 2 parameters: oldUri and newUri so the type of kind does not leak here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, I would just remove kind and use in the printing function only.

@@ -18,6 +18,20 @@ type location = {uri : string; range : range}

type documentSymbolItem = {name : string; kind : int; location : location}

type renameFile = {kind : [`rename]; oldUri : string; newUri : string}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggestion: remove kind.

"oldUri": "%s",
"newUri": "%s"
}|}
(match rf.kind with `rename -> "rename")
Copy link
Collaborator

Choose a reason for hiding this comment

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

this can go once field kind is removed, but the printing of "rename" stays.

@@ -7,6 +7,12 @@ let topLoc fname =
loc_ghost = false;
}

let isTopLoc (loc : Warnings.loc) =
Copy link
Collaborator

Choose a reason for hiding this comment

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

It might be possible to clean this up further, but looks OK for now.

@cristianoc
Copy link
Collaborator

cristianoc commented Jun 5, 2021

Indeed, it looks great!
Left some minor comments as review.

@cristianoc cristianoc merged commit c9aab8a into rescript-lang:master Jun 5, 2021
@amiralies
Copy link
Contributor Author

Thanks, I was going to bed. I see you already fixed them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants