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

🔍 Looking up ranges in structured data #162

Open
kentookura opened this issue Oct 16, 2024 · 0 comments
Open

🔍 Looking up ranges in structured data #162

kentookura opened this issue Oct 16, 2024 · 0 comments

Comments

@kentookura
Copy link

When Asai reports an error, the diagnostics are attached to the string source like this:

 → error[(Reporter.Message.Tree_not_found foo-0006)]
 ꭍ ○ when expanding tree at address `foo-0004`
 ■ ./trees/foo-0004.tree
 1 | \import{foo-0006}
   ^ Could not find tree named `foo-0006'

I find myself in the position where it would be really useful if I could use the range to look up nodes in the syntax tree.

Nick Hu once sent a patch implementing an LSP server for Forester that contained the following function:

  let inside = fun (located : 'a Range.located) ->
    match located.loc with
    | Some loc -> begin
        match Range.view loc with
        | `Range (start, end_) ->
          let within (a, b) x = a <= x && x <= b in
          within (start.line_num, end_.line_num ) (pos.line + 1)
          && within ((start.offset - start.start_of_line), (end_.offset - end_.start_of_line - 1)) pos.character
        | _ -> false
      end
    | None -> false

I did not think through the arithmetic, but conceptually, this is the right way to do this, right? Or is there a more elegant way using more library functions?

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

No branches or pull requests

1 participant