From a96532aa657ff8a670cec295e8c4e1be19a51491 Mon Sep 17 00:00:00 2001 From: Auguste Rame Date: Fri, 8 Jul 2022 10:13:46 +0200 Subject: [PATCH] Add config option for unused vars --- README.md | 21 +++++++++++--- src/Config.zig | 3 ++ src/main.zig | 72 +++++++++++++++++++++++++----------------------- src/requests.zig | 1 + 4 files changed, 58 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 6e8d061..3a33a9d 100644 --- a/README.md +++ b/README.md @@ -9,15 +9,27 @@ Zig Language Server, or `zls`, is a language server for Zig. The Zig wiki states ## Table Of Contents - [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) -- [Usage](#usage) +- [Features](#features) - [VSCode](#vscode) - [Sublime Text](#sublime-text) + - [Sublime Text 3](#sublime-text-3) + - [Sublime Text 4](#sublime-text-4) - [Kate](#kate) - [Neovim/Vim8](#neovimvim8) + - [CoC](#coc) + - [YouCompleteMe](#youcompleteme) + - [nvim-lspconfig](#nvim-lspconfig) + - [LanguageClient-neovim](#languageclient-neovim) - [Emacs](#emacs) - [Doom Emacs](#doom-emacs) + - [Spacemacs](#spacemacs) - [Related Projects](#related-projects) - [License](#license) @@ -88,6 +100,7 @@ The following options are currently available. | Option | Type | Default value | What it Does | | --- | --- | --- | --- | | `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_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 | @@ -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. - Add this snippet to `LSP's` user settings: -#### For Sublime Text 3: +#### Sublime Text 3 ```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 { diff --git a/src/Config.zig b/src/Config.zig index 4d6083c..ce7e7f8 100644 --- a/src/Config.zig +++ b/src/Config.zig @@ -3,6 +3,9 @@ /// Whether to enable snippet completions enable_snippets: bool = false, +/// Whether to enable unused variable warnings +enable_unused_variable_warnings: bool = false, + /// zig library path zig_lib_path: ?[]const u8 = null, diff --git a/src/main.zig b/src/main.zig index 7314c61..128a1e3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -229,46 +229,48 @@ fn publishDiagnostics(arena: *std.heap.ArenaAllocator, handle: DocumentStore.Han }); } - for (handle.document_scope.scopes) |scope| { - const scope_data = switch (scope.data) { - .function => |f| f, - .block => |b| b, - else => continue, - }; - - 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, + if (config.enable_unused_variable_warnings) { + for (handle.document_scope.scopes) |scope| { + const scope_data = switch (scope.data) { + .function => |f| f, + .block => |b| b, else => continue, }; - const pit_start = tree.firstToken(scope_data); - const pit_end = ast.lastToken(tree, scope_data); + var decl_iterator = scope.decls.iterator(); + while (decl_iterator.next()) |decl| { + var identifier_count: usize = 0; - 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; + 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, + }; + + 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.", - }); } } diff --git a/src/requests.zig b/src/requests.zig index 2d08466..f2cd13d 100644 --- a/src/requests.zig +++ b/src/requests.zig @@ -263,6 +263,7 @@ pub const Configuration = struct { params: struct { settings: struct { enable_snippets: ?bool, + enable_unused_variable_warnings: ?bool, zig_lib_path: ?[]const u8, zig_exe_path: ?[]const u8, warn_style: ?bool,