Giter Site home page Giter Site logo

Comments (10)

fannheyward avatar fannheyward commented on June 12, 2024

What's the output :verbose imap <Esc>? Can't reproduce, maybe caused by other vim plugin.

from coc-clangd.

ythomop avatar ythomop commented on June 12, 2024

I checked and there is no mapping. The problem, as I wrote on the OP, only happens on files that make coc-clangd kick in, except for .cpp ones. It does not happen on other file types. So, I am pretty sure it is a coc-clangd bug.

from coc-clangd.

ythomop avatar ythomop commented on June 12, 2024

I tried every possible combination of settings and I managed to stop this behavior by removing the first line of my coc-settings.json:
"coc.preferences.formatOnInsertLeave": true

From the coc.nvim documentation, this setting does the following:

"Trigger format on type when insert leave by send \n to the server."

Maybe this has to do with clang specifically?

from coc-clangd.

fannheyward avatar fannheyward commented on June 12, 2024

Reproduced.

from coc-clangd.

sam-mccall avatar sam-mccall commented on June 12, 2024

Minimal:

coc-settings.json (I needed all of these)

  "coc.preferences.formatOnInsertLeave": true,
  "coc.preferences.formatOnType": true,
  "coc.preferences.formatOnTypeFiletypes": ["cpp"],

Create file containing int;, open it.
Type ox<esc>
The new line containing x will be erased

Relevant messages:

  • textDocument/didChange, insert "x" at line 1 char 0
  • textDocument/onTypeFormatting, character is "\n"at line 1 char 1 (after 'x')
  • formatting response: delete from line 1 char 0 to line 1 char 1

Where's the bug: unclear.

onTypeFormatting is underspecified, clangd expects the client to do what vscode does: edit the document first (including any client-side autoformatting) and then send the onTypeFormatting to the server.
Here, coc is sending a character of "\n" but not editing the document to include one, which doesn't seem to be a well-defined use of the protocol. In fact, formatOnType really does claim it needs to be triggered by a character that was typed, so binding exit-insert-mode to it is always going to be a stretch.
Clangd assumes that the previous "\n" is the one that was inserted, and anything between it and the point is automatic indentation. It formats the previous line.
We could hack around this by trying to detect the coc behavior on the server side, or make coc send something a bit less ambiguous, and teach clangd to do the right thing. What if we make "ch" the empty string?

from coc-clangd.

fannheyward avatar fannheyward commented on June 12, 2024

@sam-mccall can you test the master branch? Just a quick fix to hack the ch to empty.

from coc-clangd.

fannheyward avatar fannheyward commented on June 12, 2024

Here is an issue about onTypeFormatting: neoclide/coc.nvim#1363 (comment)

from coc-clangd.

sam-mccall avatar sam-mccall commented on June 12, 2024

@fannheyward Better with that patch indeed, it doesn't destroy text :-)

And it does some formatting, not sure if it's ever going to be great as we don't know how well-formed the code is after exiting insert mode. Depends on editing flow I guess.

That will stop "regular" format-on-type from working though: if you press enter while in insert mode it'd be nice if format-on-type activated (though I can't get that to work in coc today...)

from coc-clangd.

fannheyward avatar fannheyward commented on June 12, 2024

Yes, this will stop format-on-type feature. I've revert this patch from master, and create a PR to trace it. #37 (I should do in this way from the beginning...)

from coc-clangd.

sam-mccall avatar sam-mccall commented on June 12, 2024

Yes, this will stop format-on-type feature

It actually doesn't stop format-on-type from working, clangd will reformat immediatly around the cursor if ch is sent as ''.
It's specifically ch='\n' that causes problems, because it tries to do line continuations and reindenting and stuff.

from coc-clangd.

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.