Skip to content

Commit

Permalink
completions: suggest files for include statement
Browse files Browse the repository at this point in the history
  • Loading branch information
Talv committed Sep 17, 2018
1 parent 6a2aec5 commit ee3570d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
12 changes: 6 additions & 6 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,12 @@ export class Parser {
if (!kind) {
kind = this.token();
}
const node = <Types.Literal>this.createNode(kind);
this.parseExpected(kind, undefined, false);
node.value = this.scanner.getTokenValue();
node.text = this.scanner.getTokenText();
this.nextToken();
return this.finishNode(node);
const node = <Types.Literal>this.createNode(kind, undefined, false);
node.end = this.scanner.getCurrentPos();
node.value = this.scanner.getTokenValue() || '';
node.text = this.scanner.getTokenText() || '';
this.parseExpected(kind);
return node;
}

private parseInclude(): Types.IncludeStatement {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,10 @@ export class Scanner {
return this.pos - this.char;
}

public getCurrentPos(): number {
return this.pos;
}

public getStartPos(): number {
return this.startPos;
}
Expand Down
46 changes: 42 additions & 4 deletions src/service/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as trig from '../sc2mod/trigger';

function isInComment(sourceFile: gt.SourceFile, pos: number) {
const comment = sourceFile.commentsLineMap.get(getLineAndCharacterOfPosition(sourceFile, pos).line);
return comment && pos > comment.pos;
return comment && pos >= comment.pos;
}

export const enum CompletionFunctionExpand {
Expand Down Expand Up @@ -230,11 +230,9 @@ export class CompletionsProvider extends AbstractProvider {
}

// trigger handlers
if (currentToken && currentToken.kind === gt.SyntaxKind.StringLiteral) {
if (currentToken && currentToken.kind === gt.SyntaxKind.StringLiteral && currentToken.parent.kind === gt.SyntaxKind.CallExpression) {
const callExpr = <gt.CallExpression>currentToken.parent;
// trigger handlers
if (
callExpr.kind === gt.SyntaxKind.CallExpression &&
callExpr.expression.kind === gt.SyntaxKind.Identifier &&
(<gt.Identifier>(callExpr.expression)).name === "TriggerCreate"
) {
Expand All @@ -245,6 +243,46 @@ export class CompletionsProvider extends AbstractProvider {
}
}

// include
if (currentToken && currentToken.kind === gt.SyntaxKind.StringLiteral && currentToken.pos <= position && currentToken.end >= position && currentToken.parent.kind === gt.SyntaxKind.IncludeStatement) {
const inclStmt = <gt.IncludeStatement>currentToken.parent;
const offset = position - currentToken.pos;
query = (<gt.StringLiteral>currentToken).text.substr(1, offset - 1).replace(/(\/*)[^\/]+$/, '$1');
const imap = new Map<string,lsp.CompletionItem>();

if ((<gt.StringLiteral>currentToken).text.match(/[^"]$/) || currentToken.end != position) {
for (const uri of this.store.documents.keys()) {
const meta = this.store.getDocumentMeta(uri);
if (!meta.relativeName) continue;
if (query && !meta.relativeName.toLowerCase().startsWith(query.toLowerCase())) continue;
const itemPart = meta.relativeName.substr(query.length).split('/');
if (itemPart.length > 1) {
imap.set(itemPart[0], {
kind: lsp.CompletionItemKind.Folder,
label: itemPart[0],
insertText: itemPart[0] + '/',
detail: meta.archive ? meta.archive.name : null,
data: {},
});
}
else {
imap.set(itemPart[0], {
kind: lsp.CompletionItemKind.File,
label: itemPart[0] + '.galaxy',
insertText: itemPart[0],
detail: meta.relativeName + '.galaxy',
documentation: meta.archive ? meta.archive.name : null,
data: {},
});
}
}
return {
items: Array.from(imap.values()),
isIncomplete: false,
};
}
}

// presets
if (currentToken && this.store.s2metadata) {
const elementType = this.store.s2metadata.getElementTypeOfNode(currentToken);
Expand Down
39 changes: 32 additions & 7 deletions src/service/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ export async function findWorkspaceArchive(rootPath: string) {
return null;
}

export interface SourceFileMeta {
absoluteName: string;
relativeName?: string;
archive?: SC2Archive;
}

export class Store {
private parser = new Parser();
public rootPath?: string;
Expand Down Expand Up @@ -190,24 +196,43 @@ export class Store {
await this.s2metadata.build(lang);
}

public isUriInWorkspace(documentUri: string) {
public getDocumentMeta(documentUri: string) {
let documentPath = URI.parse(documentUri).fsPath;
let meta: SourceFileMeta = {
absoluteName: documentPath,
};

const isWin = process.platform === 'win32';
if (isWin) {
documentPath = documentPath.toLowerCase();
}

if (this.rootPath && !this.s2workspace.rootArchive && documentPath.startsWith((isWin ? this.rootPath.toLowerCase() : this.rootPath) + path.sep)) {
return true;
if (this.rootPath && (!this.s2workspace || !this.s2workspace.rootArchive) && documentPath.startsWith((isWin ? this.rootPath.toLowerCase() : this.rootPath) + path.sep)) {
meta.relativeName = documentPath.substr(this.rootPath.length + 1);
}

if (this.s2workspace) {
else if (this.s2workspace) {
for (const archive of this.s2workspace.allArchives) {
if (documentPath.startsWith((isWin ? archive.directory.toLowerCase() : archive.directory) + path.sep)) return true;
if (documentPath.startsWith((isWin ? archive.directory.toLowerCase() : archive.directory) + path.sep)) {
meta.relativeName = documentPath.substr(archive.directory.length + 1);
meta.relativeName = meta.relativeName.replace(/^base\.sc2data[\/\\]/i, '');
meta.archive = archive;
break;
}
}
}

return false;
if (meta.relativeName) {
meta.relativeName = meta.relativeName.replace(/\.galaxy$/i, '');
if (isWin) {
meta.relativeName = meta.relativeName.replace(/\\/g, '/');
}
}

return meta;
}

public isUriInWorkspace(documentUri: string) {
return typeof this.getDocumentMeta(documentUri).relativeName !== 'undefined';
}

public resolveGlobalSymbol(name: string): gt.Symbol | undefined {
Expand Down

0 comments on commit ee3570d

Please sign in to comment.