Skip to content

Commit

Permalink
More refactor for ansible metadata feature (ansible#641)
Browse files Browse the repository at this point in the history
* More refactor for ansible metadata feature

*  Move the statusbar information out of `extension.ts`
   into the MetadataManager class so that all the information
   is self contained at one place
*  Use `client.isRunning()` to identify if client is running
   and get rid of the global variable use to idenity state of
   client.

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update docs

* fix typo

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ganeshrn and pre-commit-ci[bot] authored Oct 4, 2022
1 parent 8220892 commit 347055b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 87 deletions.
58 changes: 11 additions & 47 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
commands,
ExtensionContext,
extensions,
StatusBarItem,
window,
StatusBarAlignment,
workspace,
} from "vscode";
import { toggleEncrypt } from "./features/vault";
Expand All @@ -27,16 +25,11 @@ import {
showUninstallConflictsNotification,
} from "./extensionConflicts";
import { languageAssociation } from "./features/fileAssociation";
import { updateAnsibleInfo } from "./features/ansibleMetaData";
import { MetadataManager } from "./features/ansibleMetaData";

let client: LanguageClient;
let isActiveClient = false;
let cachedAnsibleVersion: string;

// status bar item
let metadataStatusBarItem: StatusBarItem;

export function activate(context: ExtensionContext): void {
export async function activate(context: ExtensionContext): Promise<void> {
new AnsiblePlaybookRunProvider(context);

// dynamically associate "ansible" language to the yaml file
Expand All @@ -53,16 +46,6 @@ export function activate(context: ExtensionContext): void {
)
);

// create a new status bar item that we can manage
metadataStatusBarItem = window.createStatusBarItem(
StatusBarAlignment.Right,
100
);
context.subscriptions.push(metadataStatusBarItem);

metadataStatusBarItem.text = cachedAnsibleVersion;
metadataStatusBarItem.show();

const serverModule = context.asAbsolutePath(
path.join("out", "server", "src", "server.js")
);
Expand Down Expand Up @@ -92,27 +75,27 @@ export function activate(context: ExtensionContext): void {
);

// start the client and the server
startClient();
await startClient();

notifyAboutConflicts();

// Update ansible meta data in the statusbar tooltip (client-server)
window.onDidChangeActiveTextEditor(updateAnsibleInfoInStatusbar);
workspace.onDidOpenTextDocument(updateAnsibleInfoInStatusbar);
// handle metadata status bar
const metaData = new MetadataManager(context, client);
metaData.updateAnsibleInfoInStatusbar();

// register ansible meta data in the statusbar tooltip (client-server)
window.onDidChangeActiveTextEditor(metaData.updateAnsibleInfoInStatusbar);
workspace.onDidOpenTextDocument(metaData.updateAnsibleInfoInStatusbar);
}

const startClient = async () => {
try {
await client.start();
isActiveClient = true;

// If the extensions change, fire this notification again to pick up on any association changes
extensions.onDidChange(() => {
notifyAboutConflicts();
});

// Update ansible meta data in the statusbar tooltip (client-server)
updateAnsibleInfoInStatusbar();
} catch (error) {
console.error("Language Client initialization failed");
}
Expand All @@ -122,7 +105,6 @@ export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
}
isActiveClient = false;
return client.stop();
}

Expand All @@ -143,7 +125,7 @@ function notifyAboutConflicts(): void {
* And resync the ansible inventory
*/
function resyncAnsibleInventory(): void {
if (isActiveClient) {
if (client.isRunning()) {
client.onNotification(
new NotificationType(`resync/ansible-inventory`),
(event) => {
Expand All @@ -153,21 +135,3 @@ function resyncAnsibleInventory(): void {
client.sendNotification(new NotificationType(`resync/ansible-inventory`));
}
}

/**
* Calls the 'updateAnsibleInfo' function to update the ansible metadata
* in the statusbar hovering action
*/
function updateAnsibleInfoInStatusbar(): void {
if (window.activeTextEditor?.document.languageId !== "ansible") {
metadataStatusBarItem.hide();
return;
}

cachedAnsibleVersion = updateAnsibleInfo(
client,
metadataStatusBarItem,
isActiveClient,
cachedAnsibleVersion
);
}
114 changes: 74 additions & 40 deletions src/features/ansibleMetaData.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,105 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { window, MarkdownString, ThemeColor, StatusBarItem } from "vscode";
import {
ExtensionContext,
window,
MarkdownString,
ThemeColor,
StatusBarItem,
StatusBarAlignment,
} from "vscode";
import { NotificationType } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";
import { formatAnsibleMetaData } from "./utils/formatAnsibleMetaData";

/**
* Sends notification with active file uri as param to the server
* and receives notification from the server with ansible meta data associated with the opened file as param
*
* @param client Language client
* @param metadataStatusBarItem Statusbar item
* @param isActiveClient Boolean representing the activation status of the client
* @param cachedAnsibleVersion String representing the cached ansible version which is updated globally
*
* @returns String representing the cached ansible version
*/
export function updateAnsibleInfo(
client: LanguageClient,
metadataStatusBarItem: StatusBarItem,
isActiveClient: boolean,
cachedAnsibleVersion: string
): string {
if (isActiveClient) {
metadataStatusBarItem.tooltip = new MarkdownString(
export class MetadataManager {
private context;
private client;
private cachedAnsibleVersion = "";
private metadataStatusBarItem: StatusBarItem;

constructor(context: ExtensionContext, client: LanguageClient) {
this.context = context;
this.client = client;

this.metadataStatusBarItem = this.initialiseStatusBar();
}

private initialiseStatusBar(): StatusBarItem {
// create a new status bar item that we can manage
const metadataStatusBarItem = window.createStatusBarItem(
StatusBarAlignment.Right,
100
);
this.context.subscriptions.push(metadataStatusBarItem);
return metadataStatusBarItem;
}

/**
* Calls the 'updateAnsibleInfo' function to update the ansible metadata
* in the statusbar hovering action
*/
public updateAnsibleInfoInStatusbar(): void {
if (window.activeTextEditor?.document.languageId !== "ansible") {
this.metadataStatusBarItem.hide();
return;
}

this.updateAnsibleInfo();
}

/**
* Sends notification with active file uri as param to the server
* and receives notification from the server with ansible meta data associated with the opened file as param
*/
public updateAnsibleInfo(): void {
if (!this.client.isRunning()) {
return;
}
this.metadataStatusBarItem.tooltip = new MarkdownString(
` $(sync~spin) Fetching... `,
true
);
metadataStatusBarItem.show();
client.onNotification(
this.metadataStatusBarItem.show();
this.client.onNotification(
new NotificationType(`update/ansible-metadata`),
(ansibleMetaDataList: any) => {
const ansibleMetaData = formatAnsibleMetaData(ansibleMetaDataList[0]);
if (ansibleMetaData.ansiblePresent) {
console.log("Ansible found in the workspace");
cachedAnsibleVersion =
this.cachedAnsibleVersion =
ansibleMetaData.metaData["ansible information"]["ansible version"];
const tooltip = ansibleMetaData.markdown;
metadataStatusBarItem.text = ansibleMetaData.eeEnabled
? `$(bracket-dot) [EE] ${cachedAnsibleVersion}`
: `$(bracket-dot) ${cachedAnsibleVersion}`;
metadataStatusBarItem.backgroundColor = "";
metadataStatusBarItem.tooltip = tooltip;
this.metadataStatusBarItem.text = ansibleMetaData.eeEnabled
? `$(bracket-dot) [EE] ${this.cachedAnsibleVersion}`
: `$(bracket-dot) ${this.cachedAnsibleVersion}`;
this.metadataStatusBarItem.backgroundColor = "";
this.metadataStatusBarItem.tooltip = tooltip;

if (!ansibleMetaData.ansibleLintPresent) {
metadataStatusBarItem.text = `$(warning) ${cachedAnsibleVersion}`;
metadataStatusBarItem.backgroundColor = new ThemeColor(
this.metadataStatusBarItem.text = `$(warning) ${this.cachedAnsibleVersion}`;
this.metadataStatusBarItem.backgroundColor = new ThemeColor(
"statusBarItem.warningBackground"
);
}

metadataStatusBarItem.show();
this.metadataStatusBarItem.show();
} else {
console.log("Ansible not found in the workspace");
metadataStatusBarItem.text = "$(error) Ansible Info";
metadataStatusBarItem.tooltip = ansibleMetaData.markdown;
metadataStatusBarItem.backgroundColor = new ThemeColor(
this.metadataStatusBarItem.text = "$(error) Ansible Info";
this.metadataStatusBarItem.tooltip = ansibleMetaData.markdown;
this.metadataStatusBarItem.backgroundColor = new ThemeColor(
"statusBarItem.errorBackground"
);
metadataStatusBarItem.show();
this.metadataStatusBarItem.show();
}
}
);
const activeFileUri = window.activeTextEditor?.document.uri.toString();
client.sendNotification(new NotificationType(`update/ansible-metadata`), [
activeFileUri,
]);
}
this.client.sendNotification(
new NotificationType(`update/ansible-metadata`),
[activeFileUri]
);

return cachedAnsibleVersion;
return;
}
}

0 comments on commit 347055b

Please sign in to comment.