Skip to content

Protols Tips

Joe Kralicky edited this page Nov 20, 2023 · 5 revisions

Protols Tips

This wiki contains general tips and opni-specific guides for using protols in vscode, as well as the standalone CLI.

Useful editor features

1. Finding References

You can find references to any message, enum, enum value, extension, or field. Simply right-click on the symbol you want to search for and select "Find References".

Examples (click to expand):

Finding references to an extension

image image

Finding references to a field literal

image image image

Finding references to a message type

image image

2. "Synthetic" Imports

Some imports do not have associated source files on disk that can be opened normally in the editor. These include the "well-known" google/protobuf/* imports, as well as imports from Go modules that do not have sources available. In these cases, protols will generate a "synthetic" source file based on the available descriptor information.

When navigating to one of these imports, for example by ctrl-clicking on the import path:

image

or a symbol that is defined in the file:

image

the synthetic source file will be seamlessly generated and opened in a read-only editor. Synthetic sources are fully interactive, just like regular source files.

3. Quick-fixes

Some quick-fixes are available and can be interacted with to fix or generate code. Available quick-fixes include:

  • Remove unused imports

    image

  • Add message definitions

    image

4. Editor Commands

A few helpful editor commands are available from the command palette:

image

  • Restart Language Server: Restarts the language server subprocess
  • Stop Language Server: Stops - and does not restart - the language server
  • Reindex Workspaces: Clears all cached data and re-scans the workspace, without restarting the actual subprocess. Useful for debugging.
  • Show Document AST: Opens a window next to the current document containing a textual representation of that document's protobuf AST. Auto-updates on save. Useful for debugging.

5. Auto-formatting

Protols will auto-format your code on save using a consistent style, similar to gofmt. In addition, the language server allows for a slightly more relaxed grammar than is defined by the protobuf spec (e.g. trailing commas, mismatched commas/semicolons, and certain empty declarations are allowed), which it will fix up during formatting. This provides a better experience while editing code, and makes common mistakes less frustrating to diagnose, like using a comma where a semicolon is expected (or vice versa), or leaving a trailing comma on the final entry of a list.

CLI Tools

There are a handful of useful CLI commands built into the protols binary.

protols vet

Similar to go vet, will perform validation and basic linting. Must be run from the workspace root.

image

protols fmt

Similar to go fmt, will auto-format any existing source files.

image

protols decode

Decodes wire-format messages using type information from the workspace. Must be run from the workspace root.

image

Useful for inspecting binary encoded web requests - see below for an example demo:

Screencast.from.2023-10-21.17-25-11.mp4
Clone this wiki locally