Code Monkey home page Code Monkey logo

Comments (16)

6cdh avatar 6cdh commented on August 9, 2024 2

Oh, it's a bug. I thought it's a feature that it reformat the whole document.

from racket-langserver.

6cdh avatar 6cdh commented on August 9, 2024 1

After some search, I found only a little editor supports onTypeFormatting:

  • vscode - cursor position, code here and getPosition
  • neovim - no support
  • helix - no support
  • emacs lsp mode - cursor position, code here

I think they all looked into vscode's implementation. So a possible solution is to assume it's cursor position.

from racket-langserver.

dannypsnl avatar dannypsnl commented on August 9, 2024 1

Agree

from racket-langserver.

jryans avatar jryans commented on August 9, 2024 1

Sounds like a good approach to me, thanks for working on this. 😄

from racket-langserver.

dannypsnl avatar dannypsnl commented on August 9, 2024

Interesting, in doc.rkt:190-208 should already make a filter

from racket-langserver.

dannypsnl avatar dannypsnl commented on August 9, 2024

If there can have a bug, it should be doc-find-containing-paren

from racket-langserver.

6cdh avatar 6cdh commented on August 9, 2024

I think I know where the bug come from.
From specification - DocumentOnTypeFormattingParams:

/**
 * The position around which the on type formatting should happen.
 * This is not necessarily the exact position where the character denoted
 * by the property `ch` got typed.
 */
position: Position;

The position could not be the position of the inserted character. It's an actually useless parameter.
But in code

(doc-line/ch this-doc (or (doc-find-containing-paren this-doc pos) 0)))

It uses pos to find the innermost pair of parentheses that contains the position pos. It can be wrong as the reason above.

In the example code, the position that the client passed is this:

(publisher))
            ^

Our code should not depend on the precise definition of position.

from racket-langserver.

dannypsnl avatar dannypsnl commented on August 9, 2024

Not quite? It says the position “not necessary” be the same, but it still is the same in most cases, where you point out the position is after the inserted, I think that’s correct.

Maybe we can use pos-1 instead of pos?

Another solution should be we stop using find parentheses, but the find prev sexp and find next sexp to detect the scope?

from racket-langserver.

6cdh avatar 6cdh commented on August 9, 2024

No. The current code expects position is here:

(publisher))
           ^

This is the position of ch in the request.

Suppose position can be position-of(ch) or position-of(ch) + 1, then we can't process this case:

)))
 ^
 ch

It would be ambiguous. This is a problem.

from racket-langserver.

dannypsnl avatar dannypsnl commented on August 9, 2024

Yes, but I think a finite scope is still better than the whole file.

from racket-langserver.

6cdh avatar 6cdh commented on August 9, 2024

I have an evil idea. We can save the position information in did-change!, and use it in on-type-formatting. So we can do precise formatting.
Another normal idea is, because we only format from line x to line y currently instead of character level, we can find the last closed parenthese at this line, and find where its matching open parenthese at.

from racket-langserver.

6cdh avatar 6cdh commented on August 9, 2024

The second idea can have a condition, that if text[position] has an closed parenthese, then use doc-find-containing-paren.

from racket-langserver.

6cdh avatar 6cdh commented on August 9, 2024

I prefer the evil solution. We just need to add a last-closed-paren field in struct Doc, and maintain it.

from racket-langserver.

jryans avatar jryans commented on August 9, 2024

I was curious what other lang servers do for this case...

Looking at Rust Analyzer, it seems like they assume that for on-type formatting, you can find a precise position by subtracting one character from the position in the protocol message. It seems to work for this one example here... but of course, I'm not sure what other clients might do.

If we really can't depend on the position field, then perhaps one of @6cdh's ideas is they way to go.

from racket-langserver.

dannypsnl avatar dannypsnl commented on August 9, 2024

Rust analyzer assume there has no other plugin will affect the position

from racket-langserver.

6cdh avatar 6cdh commented on August 9, 2024

It's been 5 days since our last discussion. What are your thoughts? I think the solution that assuming it's the cursor position looks like a safe option. If you agree, I can do this today. It would be a small fix.

from racket-langserver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.