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

Code action for OPA fmt #630

Merged
merged 6 commits into from
Apr 10, 2024
Merged

Code action for OPA fmt #630

merged 6 commits into from
Apr 10, 2024

Conversation

charlieegan3
Copy link
Member

Screen.Recording.2024-04-10.at.11.31.26.mov

anderseknert and others added 3 commits April 10, 2024 11:33
Signed-off-by: Anders Eknert <anders@styra.com>
Signed-off-by: Charlie Egan <charlie@styra.com>
Signed-off-by: Charlie Egan <charlie@styra.com>
func FmtCommand(args []string) Command {
return Command{
Title: "Format using opa-fmt",
Command: "regal.fmt",
Copy link
Member

Choose a reason for hiding this comment

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

Would you happen to know if there's a well-known command name or some other mechanism at play when an IDE/editor does "format on save" via LSP? What I'm wondering is what we'd need to do to have that just work. If it's command-name-based, we'd need to adjust this. But I know way too little about LSP myself 😅

Copy link
Member

Choose a reason for hiding this comment

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

https://zed.dev/docs/configuring-zed#lsp this makes me think there's no convention...

Copy link
Member

Choose a reason for hiding this comment

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

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting

And yeah, that'll be trivial to add with this code in place as the response is the same.

Copy link
Member

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

I'm not the most competent reviewer, but I've reviewed it anyways 🙃 (except for the copied-over diff parts)

@@ -121,6 +134,8 @@ func (l *LanguageServer) Handle(
return struct{}{}, nil
}

fmt.Fprintf(l.errorLog, "%s\n", string(util.MustMarshalJSON(req)))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
fmt.Fprintf(l.errorLog, "%s\n", string(util.MustMarshalJSON(req)))
fmt.Fprintln(l.errorLog, string(util.MustMarshalJSON(req)))

(if it was desired to keep this line)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I don't think we need this tbh. Will remove, I have some ideas to improve the logging in the server, but will do in another PR.

for _, diag := range params.Context.Diagnostics {
if diag.Code == "opa-fmt" {
actions = append(actions, CodeAction{
Title: "Format using opa fmt",
Copy link
Member

Choose a reason for hiding this comment

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

This is cool ✨

Copy link
Member

@anderseknert anderseknert left a comment

Choose a reason for hiding this comment

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

Awesome! Left a few comments, but nothing big.

internal/lsp/messages.go Show resolved Hide resolved
internal/lsp/server.go Show resolved Hide resolved
internal/lsp/format.go Show resolved Hide resolved
This is not really the best place to do this, so I'll do it properly in
another PR.

Signed-off-by: Charlie Egan <charlie@styra.com>
This is now used where it's used in the spec.

Signed-off-by: Charlie Egan <charlie@styra.com>
Signed-off-by: Charlie Egan <charlie@styra.com>
@charlieegan3 charlieegan3 merged commit de1ff4b into main Apr 10, 2024
3 checks passed
@charlieegan3 charlieegan3 deleted the code-action-fmt branch April 10, 2024 12:39
charlieegan3 added a commit that referenced this pull request Apr 10, 2024
While working on #630, I needed
to be able to see requests, responses and notifications to the server
from the client and from the server to the client.

I learned that there is functionality in the jsonrpc2 lib that we use to
do this nicely. This PR implements a new logging configuration to make
use of this using an adapted version of the LogMessage function that is
found in the library and using the OnRecv and OnSend functions.

Because of this, I have removed the logging responsibility from the
server and moved it to the connection. The server now should log errors
only and that should be the default mode of operation.

Enabling verbose logging will show you all messages being sent on the
connection. ExcludeMethods and IncludeMethods can be used to filter the
stream of messages when you are interested in monitoring or ignoring
certain flows of messages. I know that this will be useful for me and I
hope that this is a sensible enough way to do this overall.

Signed-off-by: Charlie Egan <charlie@styra.com>
charlieegan3 added a commit that referenced this pull request Apr 10, 2024
* Refactor LSP logging

While working on #630, I needed
to be able to see requests, responses and notifications to the server
from the client and from the server to the client.

I learned that there is functionality in the jsonrpc2 lib that we use to
do this nicely. This PR implements a new logging configuration to make
use of this using an adapted version of the LogMessage function that is
found in the library and using the OnRecv and OnSend functions.

Because of this, I have removed the logging responsibility from the
server and moved it to the connection. The server now should log errors
only and that should be the default mode of operation.

Enabling verbose logging will show you all messages being sent on the
connection. ExcludeMethods and IncludeMethods can be used to filter the
stream of messages when you are interested in monitoring or ignoring
certain flows of messages. I know that this will be useful for me and I
hope that this is a sensible enough way to do this overall.

Signed-off-by: Charlie Egan <charlie@styra.com>

* Inline logging config

Signed-off-by: Charlie Egan <charlie@styra.com>

* Remove VerboseLogging server option for good

Also use an io.Writer for errors

Signed-off-by: Charlie Egan <charlie@styra.com>

---------

Signed-off-by: Charlie Egan <charlie@styra.com>
srenatus pushed a commit to srenatus/regal that referenced this pull request Oct 1, 2024
* WIP code action

Signed-off-by: Anders Eknert <anders@styra.com>

* Support sending of workspace/applyEdits

Signed-off-by: Charlie Egan <charlie@styra.com>

* Fix linter errors

Signed-off-by: Charlie Egan <charlie@styra.com>

* Remove logging of unknown messages

This is not really the best place to do this, so I'll do it properly in
another PR.

Signed-off-by: Charlie Egan <charlie@styra.com>

* Add OptionalVersionedTextDocumentIdentifier

This is now used where it's used in the spec.

Signed-off-by: Charlie Egan <charlie@styra.com>

* Add license for ComputeEdits

Signed-off-by: Charlie Egan <charlie@styra.com>

---------

Signed-off-by: Anders Eknert <anders@styra.com>
Signed-off-by: Charlie Egan <charlie@styra.com>
Co-authored-by: Anders Eknert <anders@styra.com>
srenatus pushed a commit to srenatus/regal that referenced this pull request Oct 1, 2024
* Refactor LSP logging

While working on StyraInc#630, I needed
to be able to see requests, responses and notifications to the server
from the client and from the server to the client.

I learned that there is functionality in the jsonrpc2 lib that we use to
do this nicely. This PR implements a new logging configuration to make
use of this using an adapted version of the LogMessage function that is
found in the library and using the OnRecv and OnSend functions.

Because of this, I have removed the logging responsibility from the
server and moved it to the connection. The server now should log errors
only and that should be the default mode of operation.

Enabling verbose logging will show you all messages being sent on the
connection. ExcludeMethods and IncludeMethods can be used to filter the
stream of messages when you are interested in monitoring or ignoring
certain flows of messages. I know that this will be useful for me and I
hope that this is a sensible enough way to do this overall.

Signed-off-by: Charlie Egan <charlie@styra.com>

* Inline logging config

Signed-off-by: Charlie Egan <charlie@styra.com>

* Remove VerboseLogging server option for good

Also use an io.Writer for errors

Signed-off-by: Charlie Egan <charlie@styra.com>

---------

Signed-off-by: Charlie Egan <charlie@styra.com>
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

Successfully merging this pull request may close these issues.

3 participants