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

Support language-server arguments in the extension. #5056

Merged
merged 4 commits into from
Mar 4, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions utils/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,60 @@ let client: LanguageClient;
/**
* Splits a CLI-style quoted string.
*/
function splitQuotedString(carbonArgs: string): string[] {
const regex = /"([^"]*)"|'([^']*)'|(\S+)/g;
const result = [];
let match;
function splitQuotedString(argsString: string): string[] {
const args: string[] = [];
let arg = '';
let inSingleQuotes = false;
let inDoubleQuotes = false;
let escaped = false;

while ((match = regex.exec(carbonArgs)) !== null) {
if (match[1]) {
result.push(match[1]);
} else if (match[2]) {
result.push(match[2]);
} else if (match[3]) {
result.push(match[3]);
for (const char of argsString) {
if (escaped) {
arg += char;
escaped = false;
continue;
}
switch (char) {
case '\\':
escaped = true;
continue;
case "'":
if (!inDoubleQuotes) {
inSingleQuotes = !inSingleQuotes;
continue;
}
break;
case '"':
if (!inSingleQuotes) {
inDoubleQuotes = !inDoubleQuotes;
continue;
}
break;
case ' ':
if (!inSingleQuotes && !inDoubleQuotes) {
args.push(arg);
arg = '';
}
break;
}
arg += char;
}
return result;

if (arg.length > 0) {
args.push(arg);
}

return args;
}
Copy link
Contributor

@zygoloid zygoloid Mar 4, 2025

Choose a reason for hiding this comment

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

Suggested change
function splitQuotedString(argsString: string): string[] {
const args: string[] = [];
let arg = '';
let inSingleQuotes = false;
let inDoubleQuotes = false;
let escaped = false;
while ((match = regex.exec(carbonArgs)) !== null) {
if (match[1]) {
result.push(match[1]);
} else if (match[2]) {
result.push(match[2]);
} else if (match[3]) {
result.push(match[3]);
for (const char of argsString) {
if (escaped) {
arg += char;
escaped = false;
continue;
}
switch (char) {
case '\\':
escaped = true;
continue;
case "'":
if (!inDoubleQuotes) {
inSingleQuotes = !inSingleQuotes;
continue;
}
break;
case '"':
if (!inSingleQuotes) {
inDoubleQuotes = !inDoubleQuotes;
continue;
}
break;
case ' ':
if (!inSingleQuotes && !inDoubleQuotes) {
args.push(arg);
arg = '';
}
break;
}
arg += char;
}
return result;
if (arg.length > 0) {
args.push(arg);
}
return args;
}
function splitQuotedString(argsString: string): string[] {
const args: string[] = [];
let arg = '';
let inSingleQuotes = false;
let inDoubleQuotes = false;
let escaped = false;
let empty = true;
for (const char of argsString) {
const was_empty = empty;
empty = false;
if (escaped) {
arg += char;
escaped = false;
continue;
}
switch (char) {
case '\\':
escaped = true;
continue;
case "'":
if (!inDoubleQuotes) {
inSingleQuotes = !inSingleQuotes;
continue;
}
break;
case '"':
if (!inSingleQuotes) {
inDoubleQuotes = !inDoubleQuotes;
continue;
}
break;
case ' ':
if (!inSingleQuotes && !inDoubleQuotes) {
if (!was_empty) {
args.push(arg);
arg = '';
}
empty = true;
continue;
}
break;
}
arg += char;
}
if (!empty) {
args.push(arg);
}
return args;
}

Some tweaks to handle "" as an argument, multiple spaces between arguments, and an argument list ending in a space.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adopting this, but in the future I'd find smaller deltas more helpful. The delta is broken and it's difficult to actually understand the changes here without applying them.

Copy link
Contributor Author

@jonmeow jonmeow Mar 4, 2025

Choose a reason for hiding this comment

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

Note, part of the issue here I think is you're [manually] making a suggestion around code that I assume GitHub was not happy to take a suggestion for.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, github's suggestion feature is a bit broken, unfortunately. I'll try to split the suggestion up into chunks in future.


/**
* Combines the `language-server` command with args from settings.
*/
function buildServerArgs(settings: WorkspaceConfiguration): string[] {
const result: string[] = [];
result.push(...splitQuotedString(settings.get('carbonServerCommandArgs', '')));
result.push(
...splitQuotedString(settings.get('carbonServerCommandArgs', ''))
);
result.push('language-server');
result.push(
...splitQuotedString(settings.get('carbonServerSubcommandArgs', ''))
Expand Down