Skip to content

Commit

Permalink
Use markdown a bit smarter in hovers
Browse files Browse the repository at this point in the history
Mark if it is a macro or not, even when it is a special form.

Fixes #1360
  • Loading branch information
PEZ committed Oct 23, 2021
1 parent eadfb06 commit dc4d072
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 99 deletions.
1 change: 0 additions & 1 deletion src/providers/hover.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as vscode from 'vscode';
import * as state from '../state';
import * as util from '../utilities';
import * as infoparser from './infoparser';
import * as namespace from '../namespace';
Expand Down
123 changes: 25 additions & 98 deletions src/providers/infoparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export class REPLInfoParser {

private _specialForm: boolean = false;

private _isMacro: boolean = false;

constructor(msg: any) {
if (msg) {
this._name = '';
Expand All @@ -30,6 +32,9 @@ export class REPLInfoParser {
this._name += '/' + msg.member;
}
}
if (msg.macro) {
this._isMacro = true;
}
if (msg["special-form"]) {
this._specialForm = true;
this._arglist = undefined;
Expand All @@ -45,86 +50,6 @@ export class REPLInfoParser {
}
}

private formatName(value: string) {
if (value && value != "") {
let result = '';
// Format the name.
result += '**' + value + '** ';
if (this._specialForm) {
result += '(special form)';
}
return result;
}
return '';
}

private formatFormsString(value: string) {
if (value && value != "") {
let result = '';
// Format the different signatures for the fn
result += value.substring(0, value.length)
.replace(/\)/g, ')')
.replace(/\(/g, '* (');
return result;
}
return '';
}

private formatArgList(value: string) {
if (value && value != "") {
let result = '';
// Format the different signatures for the fn
result += value.substring(0, value.length)
.replace(/\]/g, ']')
.replace(/\[/g, '* [');
return result;
}
return '';
}

private formatDocString(value: string, defaultValue?: string): MarkdownString {
const codeBlockRegex = /(```[a-z]*\n[\s\S]*?\n([\s]+)?```)/g;
const splitDocstring = (ds: string) => ds.split(codeBlockRegex).filter(s => s && s.trim());
const isCodeblock = (s: string) => s.match(codeBlockRegex);
const trimDocstringPadding = (s: string) => {
let min = undefined;
let lines = s.split(/\n/);
lines.forEach(l => {
const sp = l.match(/^\s+/);
if (sp) {
const len = sp[0].length
min = len < min || !min ? len : min;
}
});
if (!min) {
return s;
}
const trimmed = lines.map((l, i) => {
if (i === 0) {
return l;
}
const re = RegExp(`^\\s{${min}}`);
return l.replace(re, "");
});
return trimmed.join("\n");
}

const docString = new MarkdownString("");
if (value) {
value = trimDocstringPadding(value);
splitDocstring(value).forEach(s => {
if (isCodeblock(s)) {
docString.appendMarkdown(s);
} else {
docString.appendCodeblock(s, "text");
}
});
} else if (defaultValue) {
docString.appendText(defaultValue);
}
return docString;
}

private getParameters(symbol: string, argList: string): ParameterInformation[] {
const offsets = this.getParameterOffsets(symbol, argList);
if (offsets !== undefined) {
Expand Down Expand Up @@ -168,43 +93,45 @@ export class REPLInfoParser {
getHover(): MarkdownString {
const hover = new MarkdownString();
if (this._name !== '') {
const name = this.formatName(this._name);
hover.appendMarkdown(`${name}\n`);
if (this._specialForm) {
if (this._formsString) {
hover.appendText(this.formatFormsString(this._formsString));
hover.appendText("\n");
}
} else {
if (!this._specialForm || this._isMacro) {
hover.appendCodeblock(this._name, 'clojure');
if (this._arglist) {
const args = this.formatArgList(this._arglist);
hover.appendMarkdown(args);
hover.appendText("\n");
hover.appendCodeblock(this._arglist, 'clojure');
}
}
let docString = this.formatDocString(this._docString, "No documentation available");
hover.appendMarkdown(docString.value);
else {
if (this._formsString) {
hover.appendCodeblock(this._formsString, 'clojure');
}
}
if (this._specialForm || this._isMacro) {
hover.appendText(`${this._specialForm ? "(special form) " : ""}${this._isMacro ? "(macro)" : ""}\n`);
}
else {
hover.appendText('\n');
}
hover.appendMarkdown(this._docString);
}
return hover;
}

getHoverNotAvailable() {
let result = '';
if (this._name !== '') {
result += this.formatName(this._name);
result += this._name;
result += '\n';
result += 'No information available';
}
return result;
}

getCompletion(): [string | MarkdownString, string] {
const name = new MarkdownString(this._docString);
if (this._name !== '') {
let docString = this.formatDocString(this._docString);
if (this._specialForm) {
return [docString, this._formsString];
return [name, this._formsString];
} else {
return [docString, this._arglist];
return [name, this._arglist];
}
}
return [undefined, undefined];
Expand All @@ -224,7 +151,7 @@ export class REPLInfoParser {
signature.parameters = this.getParameters(symbol, argList);
}
if (this._docString && getConfig().showDocstringInParameterHelp) {
signature.documentation = this.formatDocString(this._docString);
signature.documentation = new MarkdownString(this._docString);
}
return signature;
}
Expand Down

0 comments on commit dc4d072

Please sign in to comment.