/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { CompletionItemProvider, CompletionItem, CompletionItemKind, CancellationToken, TextDocument, Position, Range, TextEdit, workspace, CompletionContext } from 'vscode'; import phpGlobals = require('./phpGlobals'); import phpGlobalFunctions = require('./phpGlobalFunctions'); export default class PHPCompletionItemProvider implements CompletionItemProvider { public provideCompletionItems(document: TextDocument, position: Position, _token: CancellationToken, context: CompletionContext): Promise { let result: CompletionItem[] = []; let shouldProvideCompletionItems = workspace.getConfiguration('php').get('suggest.basic', true); if (!shouldProvideCompletionItems) { return Promise.resolve(result); } let range = document.getWordRangeAtPosition(position); let prefix = range ? document.getText(range) : ''; if (!range) { range = new Range(position, position); } if (context.triggerCharacter === '>') { const twoBeforeCursor = new Position(position.line, Math.max(0, position.character - 2)); const previousTwoChars = document.getText(new Range(twoBeforeCursor, position)); if (previousTwoChars !== '->') { return Promise.resolve(result); } } let added: any = {}; let createNewProposal = function (kind: CompletionItemKind, name: string, entry: phpGlobals.IEntry | null): CompletionItem { let proposal: CompletionItem = new CompletionItem(name); proposal.kind = kind; if (entry) { if (entry.description) { proposal.documentation = entry.description; } if (entry.signature) { proposal.detail = entry.signature; } } return proposal; }; let matches = (name: string) => { return prefix.length === 0 || name.length >= prefix.length && name.substr(0, prefix.length) === prefix; }; if (matches('php') && range.start.character >= 2) { let twoBeforePosition = new Position(range.start.line, range.start.character - 2); let beforeWord = document.getText(new Range(twoBeforePosition, range.start)); if (beforeWord === '