Skip to content

Commit

Permalink
ability to highlight global variables (#655)
Browse files Browse the repository at this point in the history
anything declared with "var" at the root level
  • Loading branch information
llogick authored Oct 10, 2022
1 parent f204f34 commit 19fd17f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ The following options are currently available.
|`include_at_in_builtins`|`bool`|`false`| Whether the @ sign should be part of the completion of builtins.
|`max_detail_length`|`usize`|`1024 * 1024`| The detail field of completions is truncated to be no longer than this (in bytes).
| `skip_std_references` | `bool` | `false` | When true, skips searching for references in std. Improves lookup speed for functions in user's code. Renaming and go-to-definition will continue to work as is.
| `highlight_global_var_declarations` | `bool` | `false` | Whether to highlight global var declarations.

### Per-build Configuration Options

Expand Down
5 changes: 5 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@
"description": "When true, skips searching for references in std. Improves lookup speed for functions in user's code. Renaming and go-to-definition will continue to work as is",
"type": "boolean",
"default": "false"
},
"highlight_global_var_declarations": {
"description": "Whether to highlight global var declarations",
"type": "boolean",
"default": "false"
}
}
}
3 changes: 3 additions & 0 deletions src/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ skip_std_references: bool = false,
/// Path to "builtin;" useful for debugging, automatically set if let null
builtin_path: ?[]const u8 = null,

/// Whether to highlight global var declarations.
highlight_global_var_declarations: bool = false,

pub fn loadFromFile(allocator: std.mem.Allocator, file_path: []const u8) ?Config {
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();
Expand Down
63 changes: 46 additions & 17 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ fn publishDiagnostics(server: *Server, writer: anytype, handle: *DocumentStore.H
});
}

for (handle.cimports) |cimport| {
if (cimport.result != .failure) continue;
const stderr = std.mem.trim(u8, cimport.result.failure, " ");

var pos_and_diag_iterator = std.mem.split(u8, stderr, ":");
_ = pos_and_diag_iterator.next(); // skip file path
_ = pos_and_diag_iterator.next(); // skip line
_ = pos_and_diag_iterator.next(); // skip character

try diagnostics.append(allocator, .{
.range = offsets.nodeToRange(handle.tree, cimport.node, server.offset_encoding),
.severity = .Error,
.code = "cImport",
.source = "zls",
.message = try allocator.dupe(u8, pos_and_diag_iterator.rest()),
});
}

if (server.config.enable_ast_check_diagnostics and tree.errors.len == 0) {
try getAstCheckDiagnostics(server, handle, &diagnostics);
}
Expand Down Expand Up @@ -251,23 +269,34 @@ fn publishDiagnostics(server: *Server, writer: anytype, handle: *DocumentStore.H
}
}
}

for (handle.cimports) |cimport| {
if (cimport.result != .failure) continue;
const stderr = std.mem.trim(u8, cimport.result.failure, " ");

var pos_and_diag_iterator = std.mem.split(u8, stderr, ":");
_ = pos_and_diag_iterator.next(); // skip file path
_ = pos_and_diag_iterator.next(); // skip line
_ = pos_and_diag_iterator.next(); // skip character

try diagnostics.append(allocator, .{
.range = offsets.nodeToRange(handle.tree, cimport.node, server.offset_encoding),
.severity = .Error,
.code = "cImport",
.source = "zls",
.message = try allocator.dupe(u8, pos_and_diag_iterator.rest()),
});

if (server.config.highlight_global_var_declarations) {
const main_tokens = tree.nodes.items(.main_token);
const tags = tree.tokens.items(.tag);
for (tree.rootDecls()) |decl| {
const decl_tag = tree.nodes.items(.tag)[decl];
const decl_main_token = tree.nodes.items(.main_token)[decl];

switch (decl_tag) {
.simple_var_decl,
.aligned_var_decl,
.local_var_decl,
.global_var_decl,
=> {
if (tags[main_tokens[decl]] != .keyword_var) continue; // skip anything immutable
// uncomment this to get a list :)
//log.debug("possible global variable \"{s}\"", .{tree.tokenSlice(decl_main_token + 1)});
try diagnostics.append(allocator, .{
.range = offsets.tokenToRange(tree, decl_main_token, server.offset_encoding),
.severity = .Hint,
.code = "highlight_global_var_declarations",
.source = "zls",
.message = "Global var declaration",
});
},
else => {},
}
}
}

try send(writer, server.arena.allocator(), types.Notification{
Expand Down

0 comments on commit 19fd17f

Please sign in to comment.