Skip to content

Commit

Permalink
support paste json as azapi config (#43) (#44)
Browse files Browse the repository at this point in the history
* support paste json as azapi config (#43)

* fix lint
  • Loading branch information
ms-henglu authored Aug 16, 2024
1 parent 4dae5c6 commit d76eb07
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 34 deletions.
42 changes: 19 additions & 23 deletions build/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ interface Release {
assets: Build[];
}


function getPlatform(platform: string) {
if (platform === 'win32') {
return 'windows';
Expand Down Expand Up @@ -44,26 +43,24 @@ function getArch(arch: string) {
return arch;
}


async function getRelease(): Promise<Release> {
const response = await axios.get('https://api.github.com/repos/Azure/azapi-lsp/releases', {
headers: {
},
});
if (response.status == 200 && response.data.length != 0) {
const assets: Build[] = [];
for (const i in response.data[0].assets) {
assets.push({
name: response.data[0].assets[i].name,
downloadUrl: response.data[0].assets[i].browser_download_url,
});
}
return {
version: response.data[0].name,
assets: assets,
};
const response = await axios.get('https://api.github.com/repos/Azure/azapi-lsp/releases', {
headers: {},
});
if (response.status == 200 && response.data.length != 0) {
const assets: Build[] = [];
for (const i in response.data[0].assets) {
assets.push({
name: response.data[0].assets[i].name,
downloadUrl: response.data[0].assets[i].browser_download_url,
});
}
throw new Error("no valid release")
return {
version: response.data[0].name,
assets: assets,
};
}
throw new Error('no valid release');
}

async function run(platform: string, architecture: string) {
Expand All @@ -79,7 +76,7 @@ async function run(platform: string, architecture: string) {

fs.mkdirSync(installPath);

const release = await getRelease()
const release = await getRelease();

const os = getPlatform(platform);
const arch = getArch(architecture);
Expand All @@ -95,8 +92,7 @@ async function run(platform: string, architecture: string) {
throw new Error(`Install error: no matching azapi-lsp binary for ${os}/${arch}`);
}

console.log(build)

console.log(build);

// download zip
const zipfile = path.resolve(installPath, `azapi-lsp_${release.version}.zip`);
Expand All @@ -110,7 +106,7 @@ async function run(platform: string, architecture: string) {
});

// unzip
const fileExtension = os === "windows" ? ".exe" : "";
const fileExtension = os === 'windows' ? '.exe' : '';
const binaryName = path.resolve(installPath, `azapi-lsp${fileExtension}`);
const fileReadStream = fs.createReadStream(zipfile);
const unzipPipe = unzip.Extract({ path: installPath });
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"engines": {
"npm": "~8.X",
"node": "~16.X",
"vscode": "^1.61.0"
"vscode": "^1.82.0"
},
"langServer": {
"version": "0.25.2"
Expand Down Expand Up @@ -166,7 +166,7 @@
"@types/openpgp": "^4.4.15",
"@types/semver": "^7.3.4",
"@types/unzip-stream": "^0.3.1",
"@types/vscode": "^1.61.1",
"@types/vscode": "^1.82.0",
"@types/which": "^2.0.1",
"@types/yauzl": "^2.9.1",
"@typescript-eslint/eslint-plugin": "^3.9.0",
Expand Down
51 changes: 51 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,53 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
}
stopLanguageServer();
}),
vscode.workspace.onDidChangeTextDocument(async (event: vscode.TextDocumentChangeEvent) => {
if (event.document.languageId !== 'terraform') {
return;
}

const lsClient = clientHandler.getClient();
if (!lsClient) {
return;
}

const editor = vscode.window.activeTextEditor;
if (
event.reason !== vscode.TextDocumentChangeReason.Redo &&
event.reason !== vscode.TextDocumentChangeReason.Undo &&
event.document === editor?.document &&
event.contentChanges.length === 1
) {
const contentChange = event.contentChanges[0];

// Ignore deletions and trivial changes
if (contentChange.text.length < 2 || isEmptyOrWhitespace(contentChange.text)) {
return;
}

const clipboardText = await vscode.env.clipboard.readText();

if (contentChange.text !== clipboardText) {
return;
}

try {
const result: any = await lsClient.client.sendRequest('workspace/executeCommand', {

Check warning on line 78 in src/extension.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 78 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest)

Unexpected any. Specify a different type

Check warning on line 78 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test (stable, macos-latest)

Unexpected any. Specify a different type

Check warning on line 78 in src/extension.ts

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest)

Unexpected any. Specify a different type
command: 'azapi.convertJsonToAzapi',
arguments: [`jsonContent=${clipboardText}`],
});

await editor.edit((editBuilder) => {
const startPoint = contentChange.range.start;
const endPoint = editor.selection.active;
const replaceRange = new vscode.Range(startPoint, endPoint);
editBuilder.replace(replaceRange, result.hclcontent);
});
} catch (error) {
outputChannel.appendLine(`Error converting JSON to AzApi: ${error}`);
}
}
}),
vscode.workspace.onDidChangeConfiguration(async (event: vscode.ConfigurationChangeEvent) => {
if (event.affectsConfiguration('terraform') || event.affectsConfiguration('azapi-lsp')) {
const reloadMsg = 'Reload VSCode window to apply language server changes';
Expand Down Expand Up @@ -99,3 +146,7 @@ async function stopLanguageServer() {
function enabled(): boolean {
return config('azapi').get('languageServer.external', false);
}

function isEmptyOrWhitespace(s: string): boolean {
return /^\s*$/.test(s);
}
2 changes: 1 addition & 1 deletion src/test/integration/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ suite('completion', () => {

const docUri = getDocUri('main.tf');
await open(docUri);
await new Promise(r => setTimeout(r, 1000 * 40));
await new Promise((r) => setTimeout(r, 1000 * 40));
const list = await vscode.commands.executeCommand<vscode.CompletionList>(
'vscode.executeCompletionItemProvider',
docUri,
Expand Down

0 comments on commit d76eb07

Please sign in to comment.