Skip to content

Commit

Permalink
markdown helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Slava0135 committed Jul 4, 2024
1 parent d900263 commit c52f8f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/hover-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { parseComponentSummary, parseDatafieldSummary } from './parse/summary';
import { getComponentUris } from './uri-store';
import { logger } from './logging';
import { parseDataFields } from './parse/datafield';
import { codeBlock, wrapError } from './markdown/markdown';

export function registerHoverProvider() {
return vscode.languages.registerHoverProvider('yaml', {
Expand All @@ -22,23 +23,25 @@ export function registerHoverProvider() {
if (buf) {
const summary = parseComponentSummary(buf.toString(), component.toString());
if (summary) {
contents.push(new vscode.MarkdownString("```xml\n" + summary + "\n```"));
contents.push(new vscode.MarkdownString(codeBlock('xml', summary)));
} else {
contents.push(new vscode.MarkdownString(wrapError('no description')));
}
return {
contents: contents
};
}
});
} else {
contents.push(new vscode.MarkdownString(`???`));
contents.push(new vscode.MarkdownString(wrapError('unknown component')));
}
return {
contents: contents
};
} else if (isAtComponentField(document.getText(), pos)) {
const componentName = findComponentByField(document.getText(), pos);
const datafieldName = findField(document.getText(), pos);
contents.push(new vscode.MarkdownString('**???**'));
contents.push(new vscode.MarkdownString(wrapError('unknown datafield')));
if (componentName && datafieldName) {
const uri = getComponentUris().find(uri => containsComponentDefinition(uri.toString(), componentName));
if (uri) {
Expand All @@ -47,10 +50,12 @@ export function registerHoverProvider() {
const datafield = parseDataFields(buf.toString()).find(it => it.name === datafieldName);
if (datafield) {
contents.pop();
contents.push(new vscode.MarkdownString(`**${datafield.type ?? '[unknown type]'}**`));
contents.push(new vscode.MarkdownString(`**${datafield.type ?? wrapError('unknown type')}**`));
const summary = parseDatafieldSummary(buf.toString(), datafieldName);
if (summary) {
contents.push(new vscode.MarkdownString("```xml\n" + summary + "\n```"));
contents.push(new vscode.MarkdownString(codeBlock('xml', summary)));
} else {
contents.push(new vscode.MarkdownString(wrapError('no description')));
}
}
return {
Expand Down
7 changes: 7 additions & 0 deletions src/markdown/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function wrapError(str: string): string {
return `— ${str} —`;
}

export function codeBlock(lang: string, str: string): string {
return `\`\`\`${lang}\n${str}\n\`\`\``;
}

0 comments on commit c52f8f7

Please sign in to comment.