Add config option for unused vars

This commit is contained in:
Auguste Rame 2022-07-08 10:13:46 +02:00 committed by Auguste Rame
parent 3f880a0c40
commit a96532aa65
4 changed files with 58 additions and 39 deletions

View File

@ -9,15 +9,27 @@ Zig Language Server, or `zls`, is a language server for Zig. The Zig wiki states
## Table Of Contents ## Table Of Contents
- [Installation](#installation) - [Installation](#installation)
- [Build Options](#build-options) - [Installing binaries](#installing-binaries)
- [MacOS](#macos)
- [Linux](#linux)
- [From Source](#from-source)
- [Build Options](#build-options)
- [Updating Data Files](#updating-data-files)
- [Configuration Options](#configuration-options) - [Configuration Options](#configuration-options)
- [Usage](#usage) - [Features](#features)
- [VSCode](#vscode) - [VSCode](#vscode)
- [Sublime Text](#sublime-text) - [Sublime Text](#sublime-text)
- [Sublime Text 3](#sublime-text-3)
- [Sublime Text 4](#sublime-text-4)
- [Kate](#kate) - [Kate](#kate)
- [Neovim/Vim8](#neovimvim8) - [Neovim/Vim8](#neovimvim8)
- [CoC](#coc)
- [YouCompleteMe](#youcompleteme)
- [nvim-lspconfig](#nvim-lspconfig)
- [LanguageClient-neovim](#languageclient-neovim)
- [Emacs](#emacs) - [Emacs](#emacs)
- [Doom Emacs](#doom-emacs) - [Doom Emacs](#doom-emacs)
- [Spacemacs](#spacemacs)
- [Related Projects](#related-projects) - [Related Projects](#related-projects)
- [License](#license) - [License](#license)
@ -88,6 +100,7 @@ The following options are currently available.
| Option | Type | Default value | What it Does | | Option | Type | Default value | What it Does |
| --- | --- | --- | --- | | --- | --- | --- | --- |
| `enable_snippets` | `bool` | `false` | Enables snippet completions when the client also supports them. | | `enable_snippets` | `bool` | `false` | Enables snippet completions when the client also supports them. |
| `enable_unused_variable_warnings` | `bool` | `false`| Enables warnings for local variables that aren't used. |
| `zig_lib_path` | `?[]const u8` | `null` | zig library path, e.g. `/path/to/zig/lib/zig`, used to analyze std library imports. | | `zig_lib_path` | `?[]const u8` | `null` | zig library path, e.g. `/path/to/zig/lib/zig`, used to analyze std library imports. |
| `zig_exe_path` | `?[]const u8` | `null` | zig executable path, e.g. `/path/to/zig/zig`, used to run the custom build runner. If `null`, zig is looked up in `PATH`. Will be used to infer the zig standard library path if none is provided. | | `zig_exe_path` | `?[]const u8` | `null` | zig executable path, e.g. `/path/to/zig/zig`, used to run the custom build runner. If `null`, zig is looked up in `PATH`. Will be used to infer the zig standard library path if none is provided. |
| `warn_style` | `bool` | `false` | Enables warnings for style *guideline* mismatches | | `warn_style` | `bool` | `false` | Enables warnings for style *guideline* mismatches |
@ -125,7 +138,7 @@ Install the `zls-vscode` extension from [here](https://github.com/zigtools/zls-v
- Install the `LSP` package from [here](https://github.com/sublimelsp/LSP/releases) or via Package Control. - Install the `LSP` package from [here](https://github.com/sublimelsp/LSP/releases) or via Package Control.
- Add this snippet to `LSP's` user settings: - Add this snippet to `LSP's` user settings:
#### For Sublime Text 3: #### Sublime Text 3
```json ```json
{ {
@ -141,7 +154,7 @@ Install the `zls-vscode` extension from [here](https://github.com/zigtools/zls-v
} }
``` ```
#### For Sublime Text 4: #### Sublime Text 4
```json ```json
{ {

View File

@ -3,6 +3,9 @@
/// Whether to enable snippet completions /// Whether to enable snippet completions
enable_snippets: bool = false, enable_snippets: bool = false,
/// Whether to enable unused variable warnings
enable_unused_variable_warnings: bool = false,
/// zig library path /// zig library path
zig_lib_path: ?[]const u8 = null, zig_lib_path: ?[]const u8 = null,

View File

@ -229,46 +229,48 @@ fn publishDiagnostics(arena: *std.heap.ArenaAllocator, handle: DocumentStore.Han
}); });
} }
for (handle.document_scope.scopes) |scope| { if (config.enable_unused_variable_warnings) {
const scope_data = switch (scope.data) { for (handle.document_scope.scopes) |scope| {
.function => |f| f, const scope_data = switch (scope.data) {
.block => |b| b, .function => |f| f,
else => continue, .block => |b| b,
};
var decl_iterator = scope.decls.iterator();
while (decl_iterator.next()) |decl| {
var identifier_count: usize = 0;
var name_token_index = switch (decl.value_ptr.*) {
.ast_node => |an| s: {
const an_tag = tree.nodes.items(.tag)[an];
switch (an_tag) {
.simple_var_decl => {
break :s tree.nodes.items(.main_token)[an] + 1;
},
else => continue,
}
},
.param_decl => |param| param.name_token orelse continue,
else => continue, else => continue,
}; };
const pit_start = tree.firstToken(scope_data); var decl_iterator = scope.decls.iterator();
const pit_end = ast.lastToken(tree, scope_data); while (decl_iterator.next()) |decl| {
var identifier_count: usize = 0;
for (tree.tokens.items(.tag)[pit_start..pit_end]) |tag, index| { var name_token_index = switch (decl.value_ptr.*) {
if (tag == .identifier and std.mem.eql(u8, tree.tokenSlice(pit_start + @intCast(u32, index)), tree.tokenSlice(name_token_index))) identifier_count += 1; .ast_node => |an| s: {
const an_tag = tree.nodes.items(.tag)[an];
switch (an_tag) {
.simple_var_decl => {
break :s tree.nodes.items(.main_token)[an] + 1;
},
else => continue,
}
},
.param_decl => |param| param.name_token orelse continue,
else => continue,
};
const pit_start = tree.firstToken(scope_data);
const pit_end = ast.lastToken(tree, scope_data);
for (tree.tokens.items(.tag)[pit_start..pit_end]) |tag, index| {
if (tag == .identifier and std.mem.eql(u8, tree.tokenSlice(pit_start + @intCast(u32, index)), tree.tokenSlice(name_token_index))) identifier_count += 1;
}
if (identifier_count <= 1)
try diagnostics.append(.{
.range = astLocationToRange(tree.tokenLocation(0, name_token_index)),
.severity = .Error,
.code = "unused_variable",
.source = "zls",
.message = "Unused variable! Either remove the variable or use '_ = ' on the variable to bypass this error.",
});
} }
if (identifier_count <= 1)
try diagnostics.append(.{
.range = astLocationToRange(tree.tokenLocation(0, name_token_index)),
.severity = .Error,
.code = "unused_variable",
.source = "zls",
.message = "Unused variable! Either remove the variable or use '_ = ' on the variable to bypass this error.",
});
} }
} }

View File

@ -263,6 +263,7 @@ pub const Configuration = struct {
params: struct { params: struct {
settings: struct { settings: struct {
enable_snippets: ?bool, enable_snippets: ?bool,
enable_unused_variable_warnings: ?bool,
zig_lib_path: ?[]const u8, zig_lib_path: ?[]const u8,
zig_exe_path: ?[]const u8, zig_exe_path: ?[]const u8,
warn_style: ?bool, warn_style: ?bool,