Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Agents

* Before changing plugin code, read "Working on plugins" in
`docs/contributing/contributing.md`. It is the review checklist.
* Disclose the use of AI either in the commit “Co-Authored-By” or in the PR description.
59 changes: 53 additions & 6 deletions docs/contributing/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,61 @@ Alternatively, you can build the documentation as a Nix derivation from the Flak

The documentation is also built and previewed on every PR, so you can check them from the PR status.

## Working on code actions
## Working on plugins

To make HLS easier to maintain, please follow these design guidelines when adding or modifying code actions:
To make HLS easier to maintain, please follow these design guidelines when adding or modifying plugins.

1. Prefer `ghc-exactprint` to manual text parsing.
2. Prefer `ghc-exactprint` to manual code generation.
3. Code generating actions should not try to format the generated code. Assume that the user is also leveraging HLS for automated code formatting.
4. Put new code actions in their own plugin unless they are very closely aligned with an existing code action.
### Correctness

* Match on structured diagnostics (`FileDiagnostic` and the prisms in `Development.IDE.GHC.Compat.Error`), never on the message text. Add a missing prism there.
* Work on structured values, not text. Inspect the AST or a `Type` rather than its printed form or the source text (pragmas, flags). Where there is no AST, do as little as possible, such as a text edit on the range a diagnostic gives you.
* Edit using `ghc-exactprint`. Use the helpers in `hls-exactprint-utils`.
* Keep edits minimal: change only what must change and do not format generated code.
* Do not catch `SomeException`: it swallows the async exceptions ghcide uses to cancel work. Catch the specific exception.
* No `error` and no partial functions in plugin code. Use `NonEmpty` or a pattern match as proof. Tests may use them.
* Handlers run in `ExceptT PluginError` and fail with `throwError`, using the constructor that fits (see `Ide.Plugin.Error`).

### Rules and staleness

* Handlers run constantly so run cheap applicability checks first and put work that is linear in the file in a `Rule` that caches a lookup-friendly structure.
* Go through existing rules and the VFS rather than reading and parsing files again.
* Rule results are forced, so keep pretty-printing out of them. Render as late as possible, ideally in resolve.
* Offering something (lenses, code actions) uses `useWithStale*`, so it keeps working while the file does not compile, with positions mapped through the `PositionMapping` (`toCurrentRangeE`, `fromCurrentRangeE`). Use fresh results (`useE`) where a stale answer would be wrong: rename runs only on a module that compiles.
* `DynFlags` from the `ParsedModule` include the file's `LANGUAGE` pragmas, the session's do not. Test extensions with `xopt`.

### LSP

* Resolve is for results that are expensive to compute. Build it with `mkCodeActionHandlerWithResolve` or `mkCodeActionWithResolveAndCommand` so clients without resolve support still work, and share one function between the resolve and command paths.
* Titles describe the effect for the user, not the implementation or a GHC flag. Limitations are documented in `docs/` and the plugin README.

### GHC versions

* Prefer `ghcVersion >= GHC910` to CPP when both branches typecheck. Otherwise push the CPP into a `Compat` module. Where it stays, follow `Note [Guidelines For Using CPP In GHCIDE Import Statements]` (no nesting, no `#else`, clauses at the end of the imports) and comment why it is needed.
* Use `MIN_VERSION_ghc` only where the component depends on `ghc` directly, otherwise `MIN_VERSION_GLASGOW_HASKELL`.
* A workaround for a gap in GHC or another upstream gets an upstream issue and a comment linking to it.

### Structure and reuse

* Keep rule lookups (monadic), AST transformation (pure) and printing apart, so the transformation can be unit-tested.
* Parse once into a plugin-specific type and carry it through, rather than re-inspecting the AST later. Prefer an ADT (such as GHC's `IsUnicodeSyntax`) to a `Bool` or `Maybe Int`, and `TextEdit` to `(Range, Text)`.
* Reuse before writing. Look in `Development.IDE.GHC.Compat`, `Development.IDE.GHC.Util`, `Development.IDE.Core.PluginUtils`, `Ide.PluginUtils`, `Ide.Plugin.Error` (`getNormalizedFilePathE` for `Uri` to `NormalizedFilePath`), `hls-test-utils` and sibling plugins first. Generic helpers belong in `hls-plugin-api` or `ghcide`, not copied per plugin.
* New code actions go in their own plugin unless closely aligned with an existing one.
* Keep the `Recorder (WithPriority Log)` argument of `descriptor` even when nothing is logged yet, and leave `pluginPriority` alone without a reason.
* Log with a `data Log` that has one constructor per event and a concrete payload (`LogDebugStanEnvVars envVars`), not `Log String`. Wrapped tools must not write to stdout or stderr.

### Tests

* A bugfix should come with a test that fails without it.
* Each code action has a test, including one where it must not be offered.
* Copy only the files a test needs: construct a `VirtualFileTree` and pass it to `runSessionWithTestConfig` as `testDirLocation = Right tree`. A shared `hie.yaml` over all test data makes tests flaky, because progress and diagnostics can come from the wrong file. Prefer a direct cradle (`directCradle`, `directProject`) to a cabal project when only GHC arguments matter.
* Use golden tests for large outputs, and the same expected file for the resolve and non-resolve variants (`codeActionResolveCaps`, `codeActionNoResolveCaps`).
* Handle GHC differences with `knownBrokenForGhcVersions` or `ghcVersion`, not CPP.
* Do not add waits to hide a race. Investigate, or open an issue and link it.

### Style

* Import LSP lenses as `import qualified Language.LSP.Protocol.Lens as L`.
* Prefer a blanket import of a wide HLS module (such as `Development.IDE.GHC.Compat`) over a long import list.

## Sponsorship

Expand Down
Loading