export async function activate()

in src/extension.ts [14:114]


export async function activate(context: vscode.ExtensionContext): Promise<void> {
  const manifest = context.extension.packageJSON;
  reporter = new TelemetryReporter(manifest.appInsightsConnectionString);
  context.subscriptions.push(reporter);

  reporter.sendRawTelemetryEvent('activated', {
    activationEvents: manifest.activationEvents,
    main: manifest.main,
    version: manifest.version,
  });

  const lsPath = new ServerPath(context);
  clientHandler = new ClientHandler(lsPath, outputChannel, reporter);

  // Subscriptions
  context.subscriptions.push(
    vscode.commands.registerCommand('azapi.enableLanguageServer', async () => {
      if (!enabled()) {
        const currentConfig: any = config('azapi').get('languageServer');
        currentConfig.external = true;
        await config('azapi').update('languageServer', currentConfig, vscode.ConfigurationTarget.Global);
        startLanguageServer();
      }
    }),
    vscode.commands.registerCommand('azapi.disableLanguageServer', async () => {
      if (enabled()) {
        const currentConfig: any = config('azapi').get('languageServer');
        currentConfig.external = false;
        await config('azapi').update('languageServer', currentConfig, vscode.ConfigurationTarget.Global);
        stopLanguageServer();
      }
    }),
    vscode.commands.registerCommand('azapi.showSurvey', async () => {
      ShowSurvey();
    }),
    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 (!areEqualIgnoringWhitespace(contentChange.text, clipboardText)) {
          return;
        }

        try {
          const result: any = await lsClient.client.sendRequest('workspace/executeCommand', {
            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('azapi.languageServer')) {
        const reloadMsg = 'Reload VSCode window to apply language server changes';
        const selected = await vscode.window.showInformationMessage(reloadMsg, 'Reload');
        if (selected === 'Reload') {
          vscode.commands.executeCommand('workbench.action.reloadWindow');
        }
      }
    }),
  );

  if (enabled()) {
    startLanguageServer();
  }

  if (await ShouldShowSurvey()) {
    ShowSurvey();
  }
}