1- import {
2- DefinitionProvider ,
3- TextDocument ,
4- Position ,
5- CancellationToken ,
6- Definition
7- } from "vscode" ;
1+ import * as vscode from "vscode" ;
82import * as _ from "lodash" ;
9- // import * as opn from "opn";
10- import opn from "opn" ;
11- import resources from '../../config/tips/tiat-resources.json' ;
12-
13- const urlPrefix = "https://www.terraform.io" ;
14-
15- export class TerraformResDocProvider implements DefinitionProvider {
16- public provideDefinition ( document : TextDocument , position : Position , token : CancellationToken ) : Definition {
17- const words = document . getWordRangeAtPosition ( position ) ;
18- const resName = document . getText ( words ) ;
19-
20- const found = _ . get ( resources , resName ) ;
21- const urlSuffix = found . url ?. toString ( ) || "" ;
22- var target = < string > ( urlPrefix + urlSuffix ) ;
23- if ( urlSuffix && target ) {
24- opn ( target ) ;
3+ import * as fs from "fs" ;
4+ import * as path from "path" ;
5+ import { marked } from 'marked' ;
6+ import { dispose } from "vscode-extension-telemetry-wrapper" ;
7+
8+ export class TerraformResDocProvider {
9+ // public provideDefinition(document: TextDocument, position: Position, token: CancellationToken): Definition {
10+ // const words = document.getWordRangeAtPosition(position);
11+ // const resName = document.getText(words);
12+
13+ // const found = _.get(resources, resName);
14+ // const urlSuffix = found.url?.toString() || "";
15+ // var target = <string>(urlPrefix + urlSuffix);
16+ // if (urlSuffix && target) {
17+ // opn(target);
18+ // }
19+ // return null;
20+ // }
21+
22+ public static currentProvider : TerraformResDocProvider | undefined ;
23+ private readonly _panel : vscode . WebviewPanel ;
24+ private readonly _extensionUri : vscode . Uri ;
25+ private _disposables : vscode . Disposable [ ] = [ ] ;
26+ public static readonly viewType = 'tcTerraform.doc.show.id' ;
27+
28+ public static async createOrShow ( context : vscode . ExtensionContext , resType : string ) {
29+ const column = vscode . window . activeTextEditor
30+ ? vscode . window . activeTextEditor . viewColumn
31+ : undefined ;
32+
33+
34+ const targetColumn = column + 1 ;
35+ const rightEditor = vscode . window . visibleTextEditors . find ( ( editor ) => editor . viewColumn === targetColumn ) ;
36+
37+ let newEditor = rightEditor ;
38+
39+ if ( ! newEditor ) {
40+ // new editor to the right of the current editor
41+ const tempFile = await vscode . workspace . openTextDocument ( { content : '' , language : 'plaintext' } ) ;
42+ newEditor = await vscode . window . showTextDocument ( tempFile , { viewColumn : targetColumn , preview : false } ) ;
43+ }
44+
45+ // If we already have a panel, show it.
46+ if ( TerraformResDocProvider . currentProvider ) {
47+ // TerraformResDocProvider.currentProvider._panel.reveal(targetColumn);
48+ TerraformResDocProvider . currentProvider . dispose ( ) ;
49+ return ;
50+ }
51+
52+ // Otherwise, create a new panel.
53+ const panel = vscode . window . createWebviewPanel (
54+ TerraformResDocProvider . viewType ,
55+ `Doc Definition: ${ resType } ` ,
56+ newEditor . viewColumn ,
57+ getWebviewOptions ( context . extensionUri ) ,
58+ ) ;
59+ // construct the _panel
60+ TerraformResDocProvider . currentProvider = new TerraformResDocProvider ( panel , context . extensionUri ) ;
61+ const current = TerraformResDocProvider . currentProvider ;
62+ const docsRoot = path . join ( context . extensionPath , 'config' , 'docs' , 'r' ) ;
63+ const mdResType = resType . replace ( 'tencentcloud_' , '' ) ;
64+ const markdownPath = path . join ( docsRoot , `${ mdResType } .html.markdown` ) ;
65+ if ( ! fs . existsSync ( markdownPath ) ) {
66+ console . error ( 'Can not find the markdownFile: %s' , markdownPath ) ;
67+ return ;
2568 }
26- return null ;
69+ const markdownFile = fs . readFileSync ( markdownPath , 'utf8' ) ;
70+
71+ let markdown ;
72+ try {
73+ const cleanedMarkdownFile = markdownFile . replace ( / - - - [ \s \S ] * ?- - - / , '' ) ;
74+ markdown = marked ( cleanedMarkdownFile ) ;
75+ current . _panel . webview . html = markdown ;
76+ } catch ( error ) {
77+ console . error ( 'Error processing the Markdown file:' , error ) ;
78+ return ;
79+ }
80+ // Listen for when the panel is disposed
81+ current . _panel . onDidDispose ( ( ) => current . dispose ( ) , null , current . _disposables ) ;
82+ }
83+
84+ dispose ( ) {
85+ TerraformResDocProvider . currentProvider = undefined ;
86+
87+ // Clean up our resources
88+ this . _panel . dispose ( ) ;
89+
90+ while ( this . _disposables . length ) {
91+ const x = this . _disposables . pop ( ) ;
92+ if ( x ) {
93+ x . dispose ( ) ;
94+ }
95+ }
96+ }
97+
98+ public constructor ( panel ?: vscode . WebviewPanel , extensionUri ?: vscode . Uri ) {
99+ this . _panel = panel ;
100+ this . _extensionUri = extensionUri ;
27101 }
28- }
102+ }
103+
104+ function getWebviewOptions ( extensionUri : vscode . Uri ) : vscode . WebviewOptions {
105+ return {
106+ // Enable javascript in the webview
107+ enableScripts : true ,
108+
109+ // And restrict the webview to only loading content from our extension's `media` directory.
110+ localResourceRoots : [ vscode . Uri . joinPath ( extensionUri , 'media' ) ]
111+ } ;
112+ }
113+
0 commit comments