Skip to content

Commit

Permalink
Add new autocomplete trigger ('\') and correctly identify constructor…
Browse files Browse the repository at this point in the history
… in completion items list.
  • Loading branch information
PranayAgarwal committed Nov 8, 2016
1 parent 4f4681b commit 0911855
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(HACK_MODE, new providers.HackDocumentSymbolProvider()));
context.subscriptions.push(vscode.languages.registerWorkspaceSymbolProvider(new providers.HackWorkspaceSymbolProvider()));
context.subscriptions.push(vscode.languages.registerDocumentHighlightProvider(HACK_MODE, new providers.HackDocumentHighlightProvider()));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(HACK_MODE, new providers.HackCompletionItemProvider(), '$', '>', ':'));
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(HACK_MODE, new providers.HackCompletionItemProvider(), '$', '>', ':', '\\'));
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(HACK_MODE, new providers.HackDocumentFormattingEditProvider()));
context.subscriptions.push(vscode.languages.registerReferenceProvider(HACK_MODE, new providers.HackReferenceProvider()));
context.subscriptions.push(vscode.languages.registerDefinitionProvider(HACK_MODE, new providers.HackDefinitionProvider()));
Expand Down
16 changes: 10 additions & 6 deletions src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,14 @@ export class HackCompletionItemProvider implements vscode.CompletionItemProvider
kind = vscode.CompletionItemKind.Variable;
labelType = labelType.split('\\').pop();
} else if (labelType.startsWith('(function')) {
// If the name and return type matches then it is a constructor
if (element.name === element.func_details.return_type) {
kind = vscode.CompletionItemKind.Constructor;
} else {
kind = vscode.CompletionItemKind.Method;
}
const typeSplit = labelType.slice(1, labelType.length - 1).split(':');
labelType = typeSplit[0] + ': ' + typeSplit[1].split('\\').pop();
kind = vscode.CompletionItemKind.Method;
} else if (labelType === 'class') {
kind = vscode.CompletionItemKind.Class;
}
Expand Down Expand Up @@ -163,9 +168,8 @@ export class HackDocumentFormattingEditProvider implements vscode.DocumentFormat
}

export class HackReferenceProvider implements vscode.ReferenceProvider {
public provideReferences(document: vscode.TextDocument, position: vscode.Position, context: vscode.ReferenceContext,
token: vscode.CancellationToken)
: Thenable<vscode.Location[]> {
public provideReferences(document: vscode.TextDocument, position: vscode.Position, context: vscode.ReferenceContext, token: vscode.CancellationToken)
: Thenable<vscode.Location[]> {
const text = document.getText();
return hh_client.findLvarRefs(text, position.line + 1, position.character + 1).then(lvarRefs => {
return hh_client.ideFindRefs(text, position.line + 1, position.character + 1).then(findRefs => {
Expand Down Expand Up @@ -205,13 +209,13 @@ export class HackReferenceProvider implements vscode.ReferenceProvider {

export class HackDefinitionProvider implements vscode.DefinitionProvider {
public provideDefinition(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken)
: Thenable<vscode.Definition> {
: Thenable<vscode.Definition> {
const text = document.getText();
return hh_client.ideGetDefinition(text, position.line + 1, position.character + 1).then(value => {
const definition: vscode.Location[] = [];
value.forEach(element => {
if (element.definition_pos != null) {
const location : vscode.Location = new vscode.Location(
const location: vscode.Location = new vscode.Location(
vscode.Uri.file(element.definition_pos.filename || document.fileName),
new vscode.Range(
new vscode.Position(element.definition_span.line_start - 1, element.definition_span.char_start - 1),
Expand Down
51 changes: 35 additions & 16 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,46 @@ export function ideGetDefinition(text: string, line: number, character: number)
filename: string,
line: number,
char_start: number,
char_end: number },
definition_pos: {
filename: string,
line: number,
char_start: number,
char_end: number
},
definition_span: {
filename: string,
line_start: number,
char_start: number,
line_end: number,
char_end: number
},
definition_id: number
char_end: number
},
definition_pos: {
filename: string,
line: number,
char_start: number,
char_end: number
},
definition_span: {
filename: string,
line_start: number,
char_start: number,
line_end: number,
char_end: number
},
definition_id: number
}[]> {
return run(['--ide-get-definition', line + ':' + character], text);
}

export function autoComplete(text: string, position: number)
: Thenable<{ name: string, type: string }[]> { // tslint:disable-line
: Thenable<{
name: string,
type: string, // tslint:disable-line
pos: {
fileName: string,
line: number,
char_start: number,
char_end: number
},
func_details: {
min_arity: number,
return_type: string,
params: {
name: string,
type: string, // tslint:disable-line
variadic: boolean
}[]
}
}[]> {
// Insert hh_client autocomplete token at cursor position.
const autoTok: string = 'AUTO332';
const input = [text.slice(0, position), autoTok, text.slice(position)].join('');
Expand Down

0 comments on commit 0911855

Please sign in to comment.