Skip to content

Commit

Permalink
fix doc issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lyu571 committed Dec 11, 2023
1 parent f0c18d2 commit 510986d
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 37 deletions.
24 changes: 19 additions & 5 deletions package-lock.json

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

26 changes: 23 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
"when": "view == tcTerraform.resourcesExplorer.cvm",
"group": "navigation"
}
],
"editor/context": [
{
"command": "tcTerraform.doc.show",
"group": "navigation"
}
]
},
"viewsContainers": {
Expand Down Expand Up @@ -164,12 +170,25 @@
{
"command": "tcTerraformer.import",
"title": "Import",
"category": "TencentCloud Terraformer"
"category": "Hidden"
},
{
"command": "tcTerraformer.plan",
"title": "Plan",
"category": "TencentCloud Terraformer"
"category": "Hidden"
},
{
"command": "tcTerraform.doc.show",
"title": "Go to Terraform Definition",
"category": "Hidden"
}
],
"keybindings": [
{
"command": "tcTerraform.doc.show",
"key": "ctrl+alt+d",
"mac": "cmd+alt+d",
"when": "editorTextFocus"
}
],
"configuration": {
Expand Down Expand Up @@ -241,6 +260,7 @@
"@types/fs-extra": "^11.0.1",
"@types/glob": "^8.0.1",
"@types/lodash": "^4.14.191",
"@types/marked": "^6.0.0",
"@types/mocha": "^10.0.1",
"@types/node": "16.x",
"@types/opn": "^3.0.28",
Expand Down Expand Up @@ -270,7 +290,7 @@
"jsdom": "^22.1.0",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"marked": "^0.3.6",
"marked": "^11.0.1",
"opn": "^6.0.0",
"reflect-metadata": "^0.1.13",
"shared": "^0.2.0",
Expand Down
135 changes: 110 additions & 25 deletions src/autocomplete/TerraformResDocProvider.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,113 @@
import {
DefinitionProvider,
TextDocument,
Position,
CancellationToken,
Definition
} from "vscode";
import * as vscode from "vscode";
import * as _ from "lodash";
// import * as opn from "opn";
import opn from "opn";
import resources from '../../config/tips/tiat-resources.json';

const urlPrefix = "https://www.terraform.io";

export class TerraformResDocProvider implements DefinitionProvider {
public provideDefinition(document: TextDocument, position: Position, token: CancellationToken): Definition {
const words = document.getWordRangeAtPosition(position);
const resName = document.getText(words);

const found = _.get(resources, resName);
const urlSuffix = found.url?.toString() || "";
var target = <string>(urlPrefix + urlSuffix);
if (urlSuffix && target) {
opn(target);
import * as fs from "fs";
import * as path from "path";
import { marked } from 'marked';
import { dispose } from "vscode-extension-telemetry-wrapper";

export class TerraformResDocProvider {
// public provideDefinition(document: TextDocument, position: Position, token: CancellationToken): Definition {
// const words = document.getWordRangeAtPosition(position);
// const resName = document.getText(words);

// const found = _.get(resources, resName);
// const urlSuffix = found.url?.toString() || "";
// var target = <string>(urlPrefix + urlSuffix);
// if (urlSuffix && target) {
// opn(target);
// }
// return null;
// }

public static currentProvider: TerraformResDocProvider | undefined;
private readonly _panel: vscode.WebviewPanel;
private readonly _extensionUri: vscode.Uri;
private _disposables: vscode.Disposable[] = [];
public static readonly viewType = 'tcTerraform.doc.show.id';

public static async createOrShow(context: vscode.ExtensionContext, resType: string) {
const column = vscode.window.activeTextEditor
? vscode.window.activeTextEditor.viewColumn
: undefined;


const targetColumn = column + 1;
const rightEditor = vscode.window.visibleTextEditors.find((editor) => editor.viewColumn === targetColumn);

let newEditor = rightEditor;

if (!newEditor) {
// new editor to the right of the current editor
const tempFile = await vscode.workspace.openTextDocument({ content: '', language: 'plaintext' });
newEditor = await vscode.window.showTextDocument(tempFile, { viewColumn: targetColumn, preview: false });
}

// If we already have a panel, show it.
if (TerraformResDocProvider.currentProvider) {
// TerraformResDocProvider.currentProvider._panel.reveal(targetColumn);
TerraformResDocProvider.currentProvider.dispose();
return;
}

// Otherwise, create a new panel.
const panel = vscode.window.createWebviewPanel(
TerraformResDocProvider.viewType,
`Doc Definition: ${resType}`,
newEditor.viewColumn,
getWebviewOptions(context.extensionUri),
);
// construct the _panel
TerraformResDocProvider.currentProvider = new TerraformResDocProvider(panel, context.extensionUri);
const current = TerraformResDocProvider.currentProvider;
const docsRoot = path.join(context.extensionPath, 'config', 'docs', 'r');
const mdResType = resType.replace('tencentcloud_', '');
const markdownPath = path.join(docsRoot, `${mdResType}.html.markdown`);
if (!fs.existsSync(markdownPath)) {
console.error('Can not find the markdownFile: %s', markdownPath);
return;
}
return null;
const markdownFile = fs.readFileSync(markdownPath, 'utf8');

let markdown;
try {
const cleanedMarkdownFile = markdownFile.replace(/---[\s\S]*?---/, '');
markdown = marked(cleanedMarkdownFile);
current._panel.webview.html = markdown;
} catch (error) {
console.error('Error processing the Markdown file:', error);
return;
}
// Listen for when the panel is disposed
current._panel.onDidDispose(() => current.dispose(), null, current._disposables);
}

dispose() {
TerraformResDocProvider.currentProvider = undefined;

// Clean up our resources
this._panel.dispose();

while (this._disposables.length) {
const x = this._disposables.pop();
if (x) {
x.dispose();
}
}
}

public constructor(panel?: vscode.WebviewPanel, extensionUri?: vscode.Uri) {
this._panel = panel;
this._extensionUri = extensionUri;
}
}
}

function getWebviewOptions(extensionUri: vscode.Uri): vscode.WebviewOptions {
return {
// Enable javascript in the webview
enableScripts: true,

// And restrict the webview to only loading content from our extension's `media` directory.
localResourceRoots: [vscode.Uri.joinPath(extensionUri, 'media')]
};
}

29 changes: 25 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,38 @@ export async function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(disposableTferPlan);

// auto-complete
console.log('activate the tips(resource and options) feature');
const tipsProvider = new TerraformTipsProvider();
console.log('activate the auto-complete(resource and argument) feature');
const exampleProvider = new autocomplete.TerraformExampleProvider();
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(TF_MODE, exampleProvider, autocomplete.EXAMPLE_TRIGGER_CHARACTER));

// tips
console.log('activate the tips(options and doc) feature');
const tipsProvider = new TerraformTipsProvider();
context.subscriptions.push(
vscode.workspace.onDidChangeTextDocument((event) => {
tipsProvider.handleCharacterEvent(event);
})
);
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(TF_MODE, tipsProvider, ...TIPS_TRIGGER_CHARACTER));
context.subscriptions.push(vscode.languages.registerDefinitionProvider(TF_MODE, new TerraformResDocProvider()));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(TF_MODE, exampleProvider, autocomplete.EXAMPLE_TRIGGER_CHARACTER));

context.subscriptions.push(vscode.commands.registerCommand('tcTerraform.doc.show', () => {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return; // no editor opening
}

// get the words under current selection
const doc = editor.document;
const selection = editor.selection;
const words = doc.getWordRangeAtPosition(selection.start);
const resType = doc.getText(words);

const regex = /^tencentcloud(?:_[^\s]+)*$/;
if (!regex.test(resType)) {
return; // not match the regex
}
TerraformResDocProvider.createOrShow(context, resType);
}));

// example
console.log('activate the auto complete(example) feature');
Expand Down

0 comments on commit 510986d

Please sign in to comment.