-
Notifications
You must be signed in to change notification settings - Fork 67
Haskell Language Server (HLS) Guide
The Haskell Language Server (HLS) is a powerful tool that provides IDE-like features for Haskell development. It integrates directly with VS Code through the Haskell extension, enabling features such as code completion, error diagnostics, refactoring, and more. This guide covers setting up HLS in VS Code specifically for Courseography and highlights the most useful features to make the developer experience more enjoyable. :)
Make sure to have all the dependencies stated in the project's README. The specific steps for installing HLS in VS Code is under the Developers Section.
Category | Feature | Description |
---|---|---|
Diagnostics | GHC Compiler Errors | Displays errors and warnings from GHC. |
HLint Hints | Provides style and refactoring suggestions from HLint. | |
Cabal Parse Errors | Reports errors from Cabal configuration files. | |
Code Assistance | Auto-completion | Provides intelligent code suggestions as you type. |
Hover Documentation | Displays type signatures and documentation on hover. | |
Go to Definition | Jump to the definition of a function or type. | |
Find References | Locate all occurrences of a function, type, or variable. | |
Jump to Type Definition | Navigate to the definition of a type used in the code. | |
Refactoring | Rename Symbol | Allows renaming functions and variables project-wide. |
Extract Function | Extracts selected code into a separate function. | |
Jump to Implementation | Navigate to function implementations. | |
Code Navigation | Workspace Symbols | Search for symbols across the project. |
Document Symbols | Lists symbols defined in a module. | |
Call Hierarchy | Displays call relationships between functions. | |
Highlight References | Highlights occurrences of variables across the code. | |
Formatting & Style | Automatic Code Formatting | Uses ormolu or fourmolu for consistent code formatting. |
Organize Imports | Automatically sorts and removes unused imports. | |
Fix Module Names | Corrects mismatched module names to match file paths. | |
Selection Range | Supports incremental code selection. | |
Folding Range | Enables code folding for large sections. |
HLS continuously checks your code for errors and warnings.
Issues appear in the Problems tab (Cmd+Shift+M
/ Ctrl+Shift+M
).
Hovering over red underlined code also provides error descriptions.
To quickly jump between errors, use F8
.
HLint suggests improvements to your code style. These appear as yellow warnings, and you can apply fixes by clicking the lightbulb icon or using Quick Fix
(Cmd+.
/ Ctrl+.
).
To rename a function or variable across the project:
- Right-click the symbol.
- Select Rename Symbol (
F2
). - Enter the new name and press
Enter
.
Limitations:
- Cross-module renaming requires indexing all project components.
To extract a block of code into a separate function:
- Highlight the code.
- Right-click → Refactor… → Extract Function.
- Name the function and apply changes.
- Go to Definition: Right-click on a function/type and select Go to Definition to navigate to its source.
- Jump to Type Definition: Works similarly but navigates to the type declaration instead.
Limitations:
- Only works for local definitions.
Displays incoming and outgoing function calls:
- Right-click on a function.
- Select Show Call Hierarchy.
- Expand nodes to see relationships between functions.
Ensures that module names match file paths:
- Right-click the module declaration.
- Select Fix Module Name.
To highlight all occurrences of a variable:
- Click on a variable or function name.
- All references in the file will be highlighted automatically.
To format Haskell code:
- Right-click → Format Document (
Shift+Alt+F
). - Ensure
ormolu
orfourmolu
is installed (stack install ormolu
).
- Selection Range: Supports incremental selection of expressions or blocks.
- Folding Range: Allows collapsing and expanding large sections of code.
This does not exhaust all features of HLS, just the most relevant/useful ones for Courseography. If you want to look at other features please refer to the documentation and update this guide if anything not listed here becomes very useful.