Skip to content

Commit

Permalink
squash!
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolaRHristov committed Mar 27, 2024
1 parent dc8e37c commit 62f8691
Show file tree
Hide file tree
Showing 10 changed files with 1,065 additions and 787 deletions.
382 changes: 211 additions & 171 deletions Source/azService.ts

Large diffs are not rendered by default.

1,128 changes: 681 additions & 447 deletions Source/extension.ts

Large diffs are not rendered by default.

138 changes: 75 additions & 63 deletions Source/parser.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,93 @@
import { never } from './utils';
import { never } from "./utils";

export type TokenKind = 'subcommand' | 'argument_name' | 'argument_value' | 'comment';
export type TokenKind =
| "subcommand"
| "argument_name"
| "argument_value"
| "comment";

export interface Token {
kind: TokenKind;
offset: number;
length: number;
text: string;
kind: TokenKind;
offset: number;
length: number;
text: string;
}

export interface Argument {
name?: Token;
value?: Token;
name?: Token;
value?: Token;
}

export interface Command {
tokens: Token[];
subcommand: Token[];
arguments: Argument[];
comment?: Token;
tokens: Token[];
subcommand: Token[];
arguments: Argument[];
comment?: Token;
}

export function parse(line: string) {
const regex = /"[^"]*"|'[^']*'|\#.*|[^\s"'#]+/g;
const tokens: Token[] = [];
let subcommand = true;
let m;
while (m = regex.exec(line)) {
const text = m[0];
const length = text.length;
const isArgument = text.startsWith('-');
const isComment = text.startsWith('#');
if (isArgument || isComment) {
subcommand = false;
}
tokens.push({
kind: subcommand ? 'subcommand' : isArgument ? 'argument_name' :
isComment ? 'comment' : 'argument_value',
offset: regex.lastIndex - length,
length,
text
});
}

const command: Command = {
tokens,
subcommand: [],
arguments: []
};
const args = command.arguments;
const regex = /"[^"]*"|'[^']*'|\#.*|[^\s"'#]+/g;
const tokens: Token[] = [];
let subcommand = true;
let m;
while ((m = regex.exec(line))) {
const text = m[0];
const length = text.length;
const isArgument = text.startsWith("-");
const isComment = text.startsWith("#");
if (isArgument || isComment) {
subcommand = false;
}
tokens.push({
kind: subcommand
? "subcommand"
: isArgument
? "argument_name"
: isComment
? "comment"
: "argument_value",
offset: regex.lastIndex - length,
length,
text,
});
}

for (const token of tokens) {
switch (token.kind) {
case 'subcommand':
command.subcommand.push(token);
break;
case 'argument_name':
args.push({ name: token });
break;
case 'argument_value':
if (args.length && !('value' in args[args.length - 1])) {
args[args.length - 1].value = token;
} else {
args.push({ value: token });
}
break;
case 'comment':
command.comment = token;
break;
default:
never(token.kind);
}
}
const command: Command = {
tokens,
subcommand: [],
arguments: [],
};
const args = command.arguments;

return command;
for (const token of tokens) {
switch (token.kind) {
case "subcommand":
command.subcommand.push(token);
break;
case "argument_name":
args.push({ name: token });
break;
case "argument_value":
if (args.length && !("value" in args[args.length - 1])) {
args[args.length - 1].value = token;
} else {
args.push({ value: token });
}
break;
case "comment":
command.comment = token;
break;
default:
never(token.kind);
}
}

return command;
}

export function findNode(command: Command, offset: number) {
return command.tokens.find(token => token.offset <= offset && token.offset + token.length > offset);
return command.tokens.find(
(token) =>
token.offset <= offset && token.offset + token.length > offset,
);
}
64 changes: 32 additions & 32 deletions Source/utils.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
import * as cp from 'child_process';
import * as fs from 'fs';
import * as cp from "child_process";
import * as fs from "fs";

export interface ExecResult {
error: Error | null;
stdout: string;
stderr: string;
error: Error | null;
stdout: string;
stderr: string;
}

export function exec(command: string) {
return new Promise<ExecResult>((resolve, reject) => {
cp.exec(command, (error, stdout, stderr) => {
(error ? reject : resolve)({ error, stdout, stderr });
});
});
return new Promise<ExecResult>((resolve, reject) => {
cp.exec(command, (error, stdout, stderr) => {
(error ? reject : resolve)({ error, stdout, stderr });
});
});
}

export function realpath(path: string) {
return new Promise<string>((resolve, reject) => {
fs.realpath(path, (error, resolvedPath) => {
if (error) {
reject(error);
} else {
resolve(resolvedPath);
}
});
});
return new Promise<string>((resolve, reject) => {
fs.realpath(path, (error, resolvedPath) => {
if (error) {
reject(error);
} else {
resolve(resolvedPath);
}
});
});
}

export function exists(path: string) {
return new Promise<boolean>(resolve => {
fs.exists(path, resolve);
});
return new Promise<boolean>((resolve) => {
fs.exists(path, resolve);
});
}

export function readdir(path: string) {
return new Promise<string[]>((resolve, reject) => {
fs.readdir(path, (error, files) => {
if (error) {
reject(error);
} else {
resolve(files);
}
});
});
return new Promise<string[]>((resolve, reject) => {
fs.readdir(path, (error, files) => {
if (error) {
reject(error);
} else {
resolve(files);
}
});
});
}

export function never(n: never) {
throw new Error(`Should not happen: ${n}`);
throw new Error(`Should not happen: ${n}`);
}
42 changes: 19 additions & 23 deletions language-configuration.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
{
"comments": {
"lineComment": "#"
},
"brackets": [
["{", "}"],
["[", "]"],
["(", ")"]
],
"autoClosingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["\"", "\""],
["'", "'"]
],
"surroundingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["\"", "\""],
["'", "'"]
]
}
"comments": {
"lineComment": "#"
},
"brackets": [["{", "}"], ["[", "]"], ["(", ")"]],
"autoClosingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["\"", "\""],
["'", "'"]
],
"surroundingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["\"", "\""],
["'", "'"]
]
}
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,9 @@
],
"languages": [
{
"aliases": [
"Azure CLI Scrapbook",
"azcli"
],
"aliases": ["Azure CLI Scrapbook", "azcli"],
"configuration": "./language-configuration.json",
"extensions": [
".azcli"
],
"extensions": [".azcli"],
"id": "azcli"
}
],
Expand Down
14 changes: 7 additions & 7 deletions package.nls.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"runLineInTerminal.title": "Run Line in Terminal",
"runLineInEditor.title": "Run Line in Editor",
"toggleLiveQuery.title": "Toggle Live Query",
"installAzureCLI.title": "Install the Azure CLI",
"configuration.title": "Azure CLI Tools Configuration",
"azureCLI.showResultInNewEditor.description": "Controls whether showing the result from running an Azure CLI command in an editor should always create a new editor.",
"azureCLI.lineContinuationCharacter.description": "Override the default continuation character (backtick [`] on Windows otherwise backslash [\\]) used for multiline commands"
"runLineInTerminal.title": "Run Line in Terminal",
"runLineInEditor.title": "Run Line in Editor",
"toggleLiveQuery.title": "Toggle Live Query",
"installAzureCLI.title": "Install the Azure CLI",
"configuration.title": "Azure CLI Tools Configuration",
"azureCLI.showResultInNewEditor.description": "Controls whether showing the result from running an Azure CLI command in an editor should always create a new editor.",
"azureCLI.lineContinuationCharacter.description": "Override the default continuation character (backtick [`] on Windows otherwise backslash [\\]) used for multiline commands"
}
40 changes: 24 additions & 16 deletions syntaxes/azcli.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@
],
"repository": {
"subcommand": {
"patterns": [{
"name": "storage.type.azcli",
"match": "^([^\\s#-]-+|[^#-])+"
}]
"patterns": [
{
"name": "storage.type.azcli",
"match": "^([^\\s#-]-+|[^#-])+"
}
]
},
"argumentName": {
"patterns": [{
"name": "variable.other.azcli",
"match": "-[^\\s#]*\\s*"
}]
"patterns": [
{
"name": "variable.other.azcli",
"match": "-[^\\s#]*\\s*"
}
]
},
"argumentValueUnquoted": {
"patterns": [{
"name": "string.unquoted.azcli",
"match": "[^\\s#]+\\s*"
}]
"patterns": [
{
"name": "string.unquoted.azcli",
"match": "[^\\s#]+\\s*"
}
]
},
"argumentValueSingleQuoted": {
"name": "string.quoted.single.azcli",
Expand All @@ -63,10 +69,12 @@
]
},
"comments": {
"patterns": [{
"name": "comment.line.hash.azcli",
"match": "\\#.*"
}]
"patterns": [
{
"name": "comment.line.hash.azcli",
"match": "\\#.*"
}
]
}
},
"scopeName": "source.azcli"
Expand Down
Loading

0 comments on commit 62f8691

Please sign in to comment.