rust-lang / rust-lang/rust-analyzer

`scope_for_offset` does not make sense

Open
#4,020 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-ide C-Architecture E-hard S-actionable
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

We have scope_for_offset function, which finds expression's scope based on offset. It is used primaraly for completion, where offset is the postion of the cursor.

Currently, ExprScopes is implmented by maintaining a tree of scopes (= sets of names) and a mapping from expressions to scopes. At the moment, scope_for_offset works by, roughly, finding the nearest expression to the offset and using it's scope. The problem with this approach is that it doesn't work. here's fun example

fn main() {
  let x;
  let y;
  /* caret here */
  let z;
}

Here, we obviously want to see x and y in scope, but not z. The problem is, there are no expressions in the snippet at all, so the "find nearest expression" plan fails.

Proposed solution is to store scope origin along with each scope, and, instead of finding closest expression, look for "closest scope", where "closes scope" is actually a bunch of hand-written heuristics for various edge cases, plus the base rule about enclosing expression.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the scope_for_offset function and the ExprScopes implementation described in the issue. Reproduce the completion case with the cursor before let z, then determine the scope-origin and closest-scope behavior needed so x and y are visible while z is not.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.