Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a 'format to camelCase' codeaction for function names #679

Merged
merged 10 commits into from
Sep 30, 2022
63 changes: 62 additions & 1 deletion src/code_actions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub const Builder = struct {
.@"loop index capture" => try handleUnusedIndexCapture(builder, actions, loc),
.@"capture" => try handleUnusedCapture(builder, actions, loc),
},
.non_camelcase_fn => try handleNonCamelcaseFunction(builder, actions, loc),
.pointless_discard => try handlePointlessDiscard(builder, actions, loc),
.omit_discard => |id| switch (id) {
.@"index capture" => try handleUnusedIndexCapture(builder, actions, loc),
Expand Down Expand Up @@ -70,6 +71,34 @@ pub const Builder = struct {
}
};

fn handleNonCamelcaseFunction(builder: *Builder, actions: *std.ArrayListUnmanaged(types.CodeAction), loc: offsets.Loc) !void {
const identifier_name = offsets.locToSlice(builder.text(), loc);

const tree = builder.handle.tree;

const decl = (try analysis.lookupSymbolGlobal(
builder.document_store,
builder.arena,
builder.handle,
identifier_name,
loc.start
)) orelse return;

const name_token_idx = decl.nameToken();
const name_loc = offsets.tokenToLoc(tree, name_token_idx);

const new_text = try createCamelcaseText(builder.arena.allocator(), identifier_name);

const action1 = types.CodeAction {
.title = "make function name camelCase",
.kind = .SourceFixAll,
NuclearPhone marked this conversation as resolved.
Show resolved Hide resolved
.isPreferred = true,
.edit = try builder.createWorkspaceEdit(&.{builder.createTextEditLoc(name_loc, new_text)}),
};

try actions.append(builder.arena.allocator(), action1);
}

fn handleUnusedFunctionParameter(builder: *Builder, actions: *std.ArrayListUnmanaged(types.CodeAction), loc: offsets.Loc) !void {
const identifier_name = offsets.locToSlice(builder.text(), loc);

Expand Down Expand Up @@ -159,7 +188,7 @@ fn handleUnusedVariableOrConstant(builder: *Builder, actions: *std.ArrayListUnma

try actions.append(builder.arena.allocator(), .{
.title = "discard value",
.kind = .SourceFixAll,
.kind = .QuickFix,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be keep at SourceFixAll

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has to be resolved before i can approve

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, i'm not too familiar with git and i missed this. fixed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@NuclearPhone NuclearPhone Sep 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

.isPreferred = true,
.edit = try builder.createWorkspaceEdit(&.{builder.createTextEditPos(index, new_text)}),
});
Expand Down Expand Up @@ -233,6 +262,33 @@ fn handlePointlessDiscard(builder: *Builder, actions: *std.ArrayListUnmanaged(ty
});
}

// attempts to converts a slice of text into camelcase 'FUNCTION_NAME' -> 'functionName'
fn createCamelcaseText(allocator: std.mem.Allocator, identifier: []const u8) ![]const u8 {
var num_separators: usize = 0;

const new_text_len = identifier.len - num_separators;
var new_text = try std.ArrayListUnmanaged(u8).initCapacity(allocator, new_text_len);
errdefer new_text.deinit(allocator);

var idx: usize = 0;

// sloppy
while(idx < identifier.len) {
const c = identifier[idx];

if(c == '_') {
const c2 = identifier[idx + 1];
new_text.appendAssumeCapacity(std.ascii.toUpper(c2));
idx += 2;
} else {
new_text.appendAssumeCapacity(std.ascii.toLower(c));
idx += 1;
}
}

return new_text.toOwnedSlice(allocator);
}

// returns a discard string `\n{indent}_ = identifier_name;`
fn createDiscardText(allocator: std.mem.Allocator, identifier_name: []const u8, indent: usize) ![]const u8 {
const new_text_len = 1 + indent + "_ = ;".len + identifier_name.len;
Expand All @@ -252,6 +308,7 @@ const DiagnosticKind = union(enum) {
unused: IdCat,
pointless_discard: IdCat,
omit_discard: DiscardCat,
non_camelcase_fn: void,
unreachable_code,

const IdCat = enum {
Expand Down Expand Up @@ -284,6 +341,10 @@ const DiagnosticKind = union(enum) {
return DiagnosticKind{
.omit_discard = parseEnum(DiscardCat, msg["discard of ".len..]) orelse return null,
};
} else if (std.mem.startsWith(u8, msg, "Functions should")) {
NuclearPhone marked this conversation as resolved.
Show resolved Hide resolved
return DiagnosticKind {
.non_camelcase_fn = {},
};
}
return null;
}
Expand Down