Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add support of auto suggestion memory variable #1819

Merged
merged 15 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
cosmicshuai marked this conversation as resolved.
Show resolved Hide resolved
"this" : {"value": "", "turnCount": ""},
"turn" : {
"dialogEvent": "value",
"recognitionResult": ""
},
"dialog" :
{"item": "",
"listType": ""}
,
"intent":
{"score": ""}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

import { readFile } from 'fs';
import * as path from 'path';

import { xhr, getErrorStatusDescription } from 'request-light';
import URI from 'vscode-uri';
Expand Down Expand Up @@ -31,6 +32,7 @@ import {
convertDiagnostics,
isValid,
TRange,
loadMemoryVariavles,
} from './utils';

// define init methods call from client
Expand All @@ -43,6 +45,7 @@ export class LGServer {
protected readonly documents = new TextDocuments();
protected readonly pendingValidationRequests = new Map<string, number>();
protected LGDocuments: LGDocument[] = []; // LG Documents Store
private readonly memoryVariables: object;
cosmicshuai marked this conversation as resolved.
Show resolved Hide resolved

constructor(protected readonly connection: IConnection) {
this.documents.listen(this.connection);
Expand All @@ -65,6 +68,7 @@ export class LGServer {
codeActionProvider: false,
completionProvider: {
resolveProvider: true,
triggerCharacters: ['.'],
},
hoverProvider: true,
foldingRangeProvider: false,
Expand All @@ -85,6 +89,10 @@ export class LGServer {
}
}
});

const curPath = __dirname;
const targetPath = path.join(curPath, '../resources/memoryVariables.json');
this.memoryVariables = loadMemoryVariavles(targetPath);
}

start() {
Expand Down Expand Up @@ -194,6 +202,12 @@ export class LGServer {
* - Hi, @{name}, what's the meaning of 'state'
* - Hi---------, @{name}--------, what-------' ------s the meaning of "state"
* - <plaintext>, @{<expression>}, <plaintext><single><plaintext>------<double>
* in LG, functions and template can only be valid in expression.
* expression means a valid expression, eg: @{add(1,2)}
* single means single quote string, eg: 'hello world'
* double means double quote string, eg: "hello world"
* including single and double since "@{text}" is a string rather that expression.
* plaintext means text after dash, eg: - Today is monday
*/
let i = 0;
while (i < lineContent.length) {
Expand Down Expand Up @@ -229,6 +243,59 @@ export class LGServer {
return { matched: true, state: finalState };
}

protected findValidMemoryVariables(params: TextDocumentPositionParams): CompletionItem[] | null {
const document = this.documents.get(params.textDocument.uri);
if (!document) return null;
const position = params.position;
const range = getRangeAtPosition(document, position);
const wordAtCurRange = document.getText(range);

if (!wordAtCurRange || !wordAtCurRange.endsWith('.')) {
return null;
}

let propertyList = wordAtCurRange.split('.');
propertyList = propertyList.slice(0, propertyList.length - 1);
let tempVariable: object = this.memoryVariables;
for (const property of propertyList) {
if (property in tempVariable) {
tempVariable = tempVariable[property];
} else {
tempVariable = {};
}
}

if (!tempVariable || Object.keys(tempVariable).length === 0) {
return null;
}

if (tempVariable instanceof Object) {
const completionList: CompletionItem[] = [];
Object.keys(tempVariable).forEach(e => {
const item = {
label: e.toString(),
kind: CompletionItemKind.Property,
insertText: e.toString(),
documentation: '',
};
completionList.push(item);
});

return completionList;
} else if (typeof tempVariable === 'string') {
return [
{
label: tempVariable,
kind: CompletionItemKind.Property,
insertText: tempVariable,
documentation: '',
},
];
}

return null;
}

protected completion(params: TextDocumentPositionParams): Thenable<CompletionList | null> {
const document = this.documents.get(params.textDocument.uri);
if (!document) {
Expand Down Expand Up @@ -266,17 +333,31 @@ export class LGServer {
};
});

const completionList = completionTemplateList.concat(completionFunctionList);
const completionVariableList = this.findValidMemoryVariables(params);
const completionRootVariableList = Object.keys(this.memoryVariables).map(e => {
return {
label: e.toString(),
kind: CompletionItemKind.Property,
insertText: e.toString(),
documentation: '',
};
});

let completionList = completionTemplateList.concat(completionFunctionList);
completionList = completionList.concat(completionRootVariableList);

const matchResult = this.matchedStates(params);
// TODO: more precise match
if (
matchResult &&
matchResult.matched &&
matchResult.state &&
allowedCompletionStates.includes(matchResult.state.toLowerCase())
) {
return Promise.resolve({ isIncomplete: true, items: completionList });
if (completionVariableList !== null && completionVariableList.length > 0) {
return Promise.resolve({ isIncomplete: true, items: completionVariableList });
} else {
return Promise.resolve({ isIncomplete: true, items: completionList });
}
} else {
return Promise.resolve(null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { readFileSync } from 'fs';

import { TextDocument, Range, Position, DiagnosticSeverity, Diagnostic } from 'vscode-languageserver-types';
import {
LGResource,
Expand Down Expand Up @@ -132,3 +134,9 @@ export function updateTemplateInContent(content: string, { name, parameters = []
const resource = LGParser.parse(content);
return resource.updateTemplate(name, name, parameters, body).toString();
}

export function loadMemoryVariavles(path: string): object {
cosmicshuai marked this conversation as resolved.
Show resolved Hide resolved
const text = readFileSync(path, 'utf-8');
const varibles = JSON.parse(text);
return varibles;
}