Skip to content

Commit

Permalink
document and format with spaces snippets tree view nodes TS (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomFractals committed Apr 22, 2022
1 parent 26bf3db commit 76fedc3
Showing 1 changed file with 138 additions and 86 deletions.
224 changes: 138 additions & 86 deletions src/snippets/snippets.ts
Original file line number Diff line number Diff line change
@@ -1,112 +1,164 @@
import {
MarkdownString,
MarkdownString,
TreeItem,
TreeItemCollapsibleState,
ThemeIcon,
Uri
}
from 'vscode';
} from 'vscode';
import * as path from 'path';

/**
* Defines snippet tree item node for the snippets tree view.
*/
export class Snippet extends TreeItem {
readonly collapsibleState = TreeItemCollapsibleState.None;
readonly contextValue = 'snippet';
readonly collapsibleState = TreeItemCollapsibleState.None;
readonly contextValue = 'snippet';

/**
* Creates new snippet tree node.
*
* @param name Short snippet name.
* @param prefix Snippet prefix, or snippets extension short name.
* @param scope Snippet scope or language.
* @param snippetDescription Snippet text description.
* @param body Snippet code body.
* @param snippetFile Parent snippet file tree node.
*/
constructor(
readonly name: string,
readonly prefix: string,
readonly scope: string[],
readonly snippetDescription: string,
readonly body: string | string[],
readonly snippetFile: SnippetFile
) {
super(name);
this.scope = [snippetFile.language];
readonly name: string,
readonly prefix: string,
readonly scope: string[],
readonly snippetDescription: string,
readonly body: string | string[],
readonly snippetFile: SnippetFile
) {
super(name);

// use snippet file language for the snippet scope
this.scope = [snippetFile.language];

// show snippet prefix as description in tree node
this.description = prefix;

// create snippet body string
let snippetBody = body;
if (Array.isArray(body)) {
snippetBody = body.join('\n');
}
let snippetInfo: string = `**${this.prefix}${this.label}** _(from ${snippetFile.label})_\n___`;
if (snippetDescription && snippetDescription !== name) {
// add description
snippetInfo += `\n${snippetDescription}\n___`;
}
this.tooltip = new MarkdownString(`${snippetInfo}\n\`\`\`${snippetFile.language}\n${snippetBody}\n\`\`\``);
}

iconPath = {
light: path.join(__filename, '..', '..', 'images', 'light', 'snippet.svg'),
dark: path.join(__filename, '..', '..', 'images', 'dark', 'snippet.svg')
};
if (Array.isArray(body)) {
snippetBody = body.join('\n');
}

// create snippet title markdown
let snippetInfo: string = `**${this.prefix}${this.label}** _(from ${snippetFile.label})_\n___`;

if (snippetDescription && snippetDescription !== name) {
// add snippet description text
snippetInfo += `\n${snippetDescription}\n___`;
}

// create snippet markdown tooltip
this.tooltip = new MarkdownString(`${snippetInfo}\n\`\`\`${snippetFile.language}\n${snippetBody}\n\`\`\``);
}

// use custom snippet icons for the tree nodes display
iconPath = {
light: path.join(__filename, '..', '..', 'images', 'light', 'snippet.svg'),
dark: path.join(__filename, '..', '..', 'images', 'dark', 'snippet.svg')
};
}

/**
* Defines snippet file tree item for display in the snippets tree view.
*/
export class SnippetFile extends TreeItem {
readonly contextValue = 'snippetFile';
readonly contextValue = 'snippetFile';

/**
* Creates new snippet file tree node.
*
* @param label Snippet file name/label.
* @param filePath Full snippet file path.
* @param language Snippet file language type.
* @param collapse Collapse/expand state to show defined snippets.
*/
constructor(
readonly label: string,
readonly filePath: string,
readonly language: string,
readonly collapse: TreeItemCollapsibleState
) {
super(label);
this.iconPath = ThemeIcon.File;
this.resourceUri = Uri.file(`_.${getFileExtension(language)}`);
this.tooltip = filePath;
this.collapsibleState = collapse;
}
readonly label: string,
readonly filePath: string,
readonly language: string,
readonly collapse: TreeItemCollapsibleState
) {
super(label);
this.iconPath = ThemeIcon.File;
this.tooltip = filePath;
this.collapsibleState = collapse;

// use resource uri with `_.<languageFileExt>` hack
// to show proper language icons in the snippets tree view
this.resourceUri = Uri.file(`_.${getFileExtension(language)}`);
}
}

/**
* Defines snippets language tree item for display in the snippets tree view.
*/
export class SnippetLanguage extends TreeItem {
readonly contextValue = 'snippetLanguage';
public snippetFiles: SnippetFile[] = new Array<SnippetFile>();
constructor(readonly language: string) {
super(language);
this.iconPath = ThemeIcon.File;
this.resourceUri = Uri.file(`_.${getFileExtension(language)}`);
this.collapsibleState = TreeItemCollapsibleState.Collapsed;
this.tooltip = `${language} snippets`;
}
readonly contextValue = 'snippetLanguage';
public snippetFiles: SnippetFile[] = new Array<SnippetFile>();

/**
* Creates nee snippets language tree node for the given language.
*
* @param language Snippets language name.
*/
constructor(readonly language: string) {
super(language);
this.iconPath = ThemeIcon.File;
this.collapsibleState = TreeItemCollapsibleState.Collapsed;
this.tooltip = `${language} snippets`;

// use resource uri with `_.<languageFileExt>` hack
// to show proper language icons in the snippets tree view
this.resourceUri = Uri.file(`_.${getFileExtension(language)}`);
}
}

/**
* Maps language to file extension for the file type tree view icon loading.
* Maps text document language id to file extension for the file type tree view icon loading.
*
* @returns The file extension for the given language id.
*/
function getFileExtension(language: string): string {
let fileExtension: string = language;
// map language to file extension
switch (language) {
case 'coffeescript':
fileExtension = 'coffee';
break;
case 'csharp':
fileExtension = 'cs';
break;
case 'fsharp':
fileExtension = 'fs';
break;
case 'javascript':
fileExtension = 'js';
break;
case 'javascriptreact':
fileExtension = 'jsx';
break;
case 'powershell':
fileExtension = 'ps1';
break;
case 'python':
fileExtension = 'py';
break;
case 'stylus':
fileExtension = 'styl';
break;
case 'typescript':
fileExtension = 'ts';
break;
case 'typescriptreact':
fileExtension = 'tsx';
break;
}
return fileExtension;
let fileExtension: string = language;
// map language to file extension
switch (language) {
case 'coffeescript':
fileExtension = 'coffee';
break;
case 'csharp':
fileExtension = 'cs';
break;
case 'fsharp':
fileExtension = 'fs';
break;
case 'javascript':
fileExtension = 'js';
break;
case 'javascriptreact':
fileExtension = 'jsx';
break;
case 'powershell':
fileExtension = 'ps1';
break;
case 'python':
fileExtension = 'py';
break;
case 'stylus':
fileExtension = 'styl';
break;
case 'typescript':
fileExtension = 'ts';
break;
case 'typescriptreact':
fileExtension = 'tsx';
break;
}
return fileExtension;
}

0 comments on commit 76fedc3

Please sign in to comment.