Skip to content

Commit

Permalink
Added a simple signature provider .
Browse files Browse the repository at this point in the history
Closes #361.
  • Loading branch information
cf committed Nov 3, 2019
1 parent 20e478e commit 5e036a6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Changes to Calva.

## [Unreleased]
- [Provide argument list help as you type the function's arguments](https://github.com/BetterThanTomorrow/calva/issues/361)
- [Support special forms in editor hover/completion](https://github.com/BetterThanTomorrow/calva/issues/441)
- [Re-open REPL windows where they were last closed](https://github.com/BetterThanTomorrow/calva/issues/300)

Expand Down
3 changes: 3 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CalvaCompletionItemProvider from './providers/completion';
import TextDocumentContentProvider from './providers/content';
import HoverProvider from './providers/hover';
import { DefinitionProvider } from './providers/definition';
import { CalvaSignatureHelpProvider } from './providers/signature';
import EvaluateMiddleWare from './evaluate';
import TestRunnerMiddleWare from './testRunner';
import annotations from './providers/annotations';
Expand Down Expand Up @@ -156,6 +157,8 @@ function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(state.documentSelector, new CalvaCompletionItemProvider()));
context.subscriptions.push(vscode.languages.registerHoverProvider(state.documentSelector, new HoverProvider()));
context.subscriptions.push(vscode.languages.registerDefinitionProvider(state.documentSelector, new DefinitionProvider()));
context.subscriptions.push(vscode.languages.registerSignatureHelpProvider(state.documentSelector, new CalvaSignatureHelpProvider(), ' ', ' '));


vscode.workspace.registerTextDocumentContentProvider('jar', new TextDocumentContentProvider());

Expand Down
29 changes: 29 additions & 0 deletions src/providers/infoparser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SignatureInformation } from 'vscode';

export class REPLInfoParser {

private _name: string = undefined;
Expand Down Expand Up @@ -148,6 +150,29 @@ export class REPLInfoParser {
}
return [undefined, undefined];
}

getSignature(): SignatureInformation[] {
if (this._name !== '') {
let str = undefined;
if (this._specialForm) {
str = this._formsString;
} else {
str = this._arglist;
}
if (str) {
let list: SignatureInformation[] = [];
let values = str.split('\n');
values.forEach(value => {
value = value.trim();
if (value !== '') {
list.push(new SignatureInformation(value));
}
});
return list;
}
}
return undefined;
}
}

export function getHover(msg: any): string {
Expand All @@ -160,4 +185,8 @@ export function getHoverNotAvailable(text: string): string {

export function getCompletion(msg: any): [string, string] {
return new REPLInfoParser(msg).getCompletion();
}

export function getSignature(msg: any): SignatureInformation[] {
return new REPLInfoParser(msg).getSignature();
}
42 changes: 42 additions & 0 deletions src/providers/signature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { TextDocument, Position, CancellationToken, SignatureHelp, SignatureHelpProvider, SignatureInformation } from 'vscode';
import select from '../select';
import * as util from '../utilities';
import * as infoparser from './infoparser';

export class CalvaSignatureHelpProvider implements SignatureHelpProvider {

async provideSignatureHelp(document: TextDocument, position: Position, token: CancellationToken): Promise<SignatureHelp> {
let text = util.getWordAtPosition(document, position);

if (util.getConnectedState()) {
const ns = util.getNamespace(document),
currentFormSelection = select.getFormSelection(document, position, false),
currentForm = document.getText(currentFormSelection);

let symbol = this.getSymbol(currentForm);
if (symbol && symbol !== '') {
let client = util.getSession(util.getFileType(document));
if (client) {
await util.createNamespaceFromDocumentIfNotExists(document);
let res = await client.info(ns, symbol);
let signatures = infoparser.getSignature(res);
if(signatures) {
let help = new SignatureHelp();
help.signatures = signatures;
return(help);
}
}

}
}
return undefined;
}

private getSymbol(str: string): string {
let pos = str.indexOf(' ');
if (pos === -1) {
return undefined;
}
return str.substr(0, pos).trim();
}
}

0 comments on commit 5e036a6

Please sign in to comment.