Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

langserver: TextEdits computed from diff for gofmt #83

Merged
merged 7 commits into from
Jul 18, 2018

Conversation

keegancsmith
Copy link
Member

@keegancsmith keegancsmith commented Jan 12, 2017

Before this commit we would send an edit which deleted everything followed up by
inserting the formatted code. This lead to a bad experience in vscode. This
change implemented a more minimal number of TextEdits by using difflib. The
behaviour now matches the behaviour of vscode-go.

WIP Known Issues

  • Trailing whitespace is not completely removed
  • test harness needs updating
  • more testing in vscode
  • order of textedits may be incorrect

Fixes #76

@keegancsmith keegancsmith changed the title WIP langserver: TextEdits computed from diff for gofmt langserver: TextEdits computed from diff for gofmt Jan 13, 2017
@keegancsmith
Copy link
Member Author

I've tested this functionality a bunch. However, I've disabled the formatting test and I need to file an issue on vscode for the deletion of trailing whitespace. The reason I disabled the test is I need to make some changes to the test harness to make it possible to test, but I have done plenty of manual testing in vscode.

@keegancsmith keegancsmith requested a review from emidoots January 13, 2017 17:47
Copy link
Member

@emidoots emidoots left a comment

Choose a reason for hiding this comment

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

Neat! No complaints from me. Sorry for the late review!

@keegancsmith
Copy link
Member Author

@slimsag no worries, I've been wanting to adjust the implementation anyways to avoid the vscode bug.

@keegancsmith
Copy link
Member Author

@slimsag This PR is still very close to completion. The issue I was experiencing with it was around trailing whitespace and removing it. I don't think I'll be able to finish it off soon though, if you are interested in looking at it?

@ramya-rao-a
Copy link
Contributor

@keegancsmith @slimsag Any updates on this?

One update from VS Code is that, now even if you send the original edit that deletes everything and adds the new output, VS Code will take care of calculating the minimal edits required.

@keegancsmith
Copy link
Member Author

Great, yeah this PR has been on the backburner a long time (more than a year)! I'll see how it works in vscode today, previously I was running into a bug around text edits and trailing whitespace in a file.

One potential issue of enabling this functionality is that it just uses gofmt. However, I assume some users want goreturns or gimports instead?

@keegancsmith
Copy link
Member Author

@ramya-rao-a the current behaviour of the go-langserver is to send an edit which deleted everything followed up by inserting the formatted code. So it seems that is good for you? This PR is about sending minimal edits ourselves.

Before this commit we would send an edit which deleted everything followed up by
inserting the formatted code. This lead to a bad experience in vscode. This
change implemented a more minimal number of TextEdits by using difflib. The
behaviour now matches the behaviour of vscode-go.
@keegancsmith
Copy link
Member Author

Just tried this again, and getting some errors. I added three empty lines then formatted and got these trace logs. Likely bugs in our implementation

--> notif: textDocument/didChange: {"textDocument":{"uri":"file:///Users/keegan/go/src/github.com/sourcegraph/go-langserver/langserver/cache.go","version":2},"contentChanges":[]}
--> notif: textDocument/didChange: {"textDocument":{"uri":"file:///Users/keegan/go/src/github.com/sourcegraph/go-langserver/langserver/cache.go","version":3},"contentChanges":[{"range":{"start":{"line":158,"character":0},"end":{"line":158,"character":0}},"rangeLength":0,"text":"\n"}]}
jsonrpc2 handler: notification "textDocument/didChange" handling error: received textDocument/didChange for invalid position {'\u009e' '\x00'} on "file:///Users/keegan/go/src/github.com/sourcegraph/go-langserver/langserver/cache.go": file only has 158 lines
--> notif: textDocument/didChange: {"textDocument":{"uri":"file:///Users/keegan/go/src/github.com/sourcegraph/go-langserver/langserver/cache.go","version":4},"contentChanges":[{"range":{"start":{"line":159,"character":0},"end":{"line":159,"character":0}},"rangeLength":0,"text":"\n"}]}
jsonrpc2 handler: notification "textDocument/didChange" handling error: received textDocument/didChange for invalid position {'\u009f' '\x00'} on "file:///Users/keegan/go/src/github.com/sourcegraph/go-langserver/langserver/cache.go": file only has 158 lines
--> request #1: textDocument/formatting: {"textDocument":{"uri":"file:///Users/keegan/go/src/github.com/sourcegraph/go-langserver/langserver/cache.go"},"options":{"tabSize":4,"insertSpaces":false}}
<-- result #1: textDocument/formatting: null

@ramya-rao-a
Copy link
Contributor

I assume some users want goreturns or gimports instead?

Yes, we should come up with a good model of sending configurations over to the LS

The choice of formatting tool is one
The choice of passing flags to the formatting tool is another.
The choice to whether or not include function snippet when auto-completing (from the other PR for auto-completion) is another
Once we have linting/vetting support, they would need their own set of flags
And so on

@lloiser lloiser mentioned this pull request Mar 17, 2018
* fix uri handling for windows

the main problem was that some parts of the code converted the path to
lower case and others just passed on the path as it was. e.g
didOpen/didChange messages just used the path whereas hover used the
lower case path to read the file, so it never read the changed file from
the overlay cache!

* correctly apply changes in textDocument/didChange to the cached file

* fix formatting + some simple tests

* add a focus prop to the langserver tests to easily run a single case

* fix complete replace of code in didChange handler

* remove unnecessary focus prop
@lloiser
Copy link
Contributor

lloiser commented Jun 27, 2018

Is there anything left here that prevents this from being merged? Looking at the linked issues above it might improve things :)

@lloiser
Copy link
Contributor

lloiser commented Jul 10, 2018

I can have a look at the merge conflicts later today if you want :)

This is a bad merge! Committing since I don't have time to finish off.

Conflicts:
* langserver/config.go
* langserver/handler.go
* langserver/integration_test.go
* main.go
@lloiser
Copy link
Contributor

lloiser commented Jul 10, 2018

@keegancsmith here is a patch.txt to fix this branch :)

@dianedanlai
Copy link

dianedanlai commented Jul 10, 2018

I look forward to the changes!! May I know if we will have the change in master by this week?

@keegancsmith
Copy link
Member Author

Apologies I meant to get to this this week. I have scheduled some time on Tuesday to tackle this.

@dianedanlai
Copy link

@keegancsmith Thank you very much!!

@keegancsmith keegancsmith merged commit 0d92b4a into master Jul 18, 2018
@keegancsmith keegancsmith deleted the k/better-fmt branch July 18, 2018 15:28
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants