List:
67 | `; 68 | parser.parse(text); 69 | const { functionGroups } = parser; 70 | const expectedFunctionGroups = [ 71 | { 72 | name: '', 73 | args: [ 74 | { 75 | key: 0, 76 | start: { 77 | line: 1, 78 | character: 31 79 | }, 80 | end: { 81 | line: 1, 82 | character: 35 83 | }, 84 | name: '', 85 | kind: 'string' 86 | }, 87 | { 88 | key: 1, 89 | start: { 90 | line: 1, 91 | character: 37 92 | }, 93 | end: { 94 | line: 1, 95 | character: 46 96 | }, 97 | name: '', 98 | kind: 'array' 99 | } 100 | ], 101 | line: 1, 102 | character: 30 103 | } 104 | ]; 105 | expect(functionGroups).to.have.lengthOf(1); 106 | expect(functionGroups).to.deep.equal(expectedFunctionGroups); 107 | }); 108 | it('should save all the function groups from the text', () => { 109 | // with multiple function groups 110 | const text = ` { 189 | const text = ` { 194 | parser.parse(text); 195 | }).to.throw(); 196 | }); 197 | it('should correctly parse the text and save the function groups when php short tags are used', () => { 198 | // with short tags 199 | const text = ` 200 |Name: =ucfrst('unknown')?>
201 |Age: echo abs(-35)?>
202 | `; 203 | parser.parse(text); 204 | const { functionGroups } = parser; 205 | const expectedFunctionGroups = [ 206 | { 207 | name: '', 208 | args: [ 209 | { 210 | key: 0, 211 | start: { 212 | line: 1, 213 | character: 25 214 | }, 215 | end: { 216 | line: 1, 217 | character: 34 218 | }, 219 | name: '', 220 | kind: 'string' 221 | } 222 | ], 223 | line: 1, 224 | character: 24 225 | }, 226 | { 227 | name: '', 228 | args: [ 229 | { 230 | key: 0, 231 | start: { 232 | line: 2, 233 | character: 26 234 | }, 235 | end: { 236 | line: 2, 237 | character: 29 238 | }, 239 | name: '', 240 | kind: 'unary' 241 | } 242 | ], 243 | line: 2, 244 | character: 25 245 | } 246 | ]; 247 | expect(functionGroups).to.have.lengthOf(2); 248 | expect(functionGroups).to.deep.equal(expectedFunctionGroups); 249 | }); 250 | }); 251 | -------------------------------------------------------------------------------- /src/pipeline.js: -------------------------------------------------------------------------------- 1 | const { isDefined } = require('./utils'); 2 | 3 | /** 4 | * Pipeline class used to apply middlewares in a pipe style 5 | */ 6 | class Pipeline { 7 | constructor() { 8 | this.steps = []; 9 | } 10 | 11 | /** 12 | * Each argument can be a function or an array with the step function being 13 | * the first element and the rest of the elements are the additional args that 14 | * will be passed to the function. 15 | * They must be set in the order set by the parameters of the function 16 | * definition, the value being processed by the pipe wil be set as the first 17 | * arg when the function is called. 18 | * 19 | * @param {(Function|(any)[])[]} steps 20 | */ 21 | pipe(...steps) { 22 | steps.forEach(step => { 23 | if (!isDefined(step)) return; 24 | 25 | let finalStep; 26 | 27 | if (Array.isArray(step)) { 28 | finalStep = step; 29 | } else { 30 | finalStep = [step]; 31 | } 32 | 33 | this.steps.push(finalStep); 34 | }); 35 | 36 | return this; 37 | } 38 | 39 | /** 40 | * Clear existing pipes 41 | */ 42 | clear() { 43 | this.steps = []; 44 | return this; 45 | } 46 | 47 | /** 48 | * The value to be processed by the pipeline 49 | * 50 | * @param {any} value 51 | * @param {boolean} clearAfter the pipes after computing the value 52 | */ 53 | async process(value, clearAfter = false) { 54 | let currentValue = value; 55 | for (const [step, ...additionalArgs] of this.steps) { 56 | currentValue = await step(currentValue, ...additionalArgs); 57 | } 58 | 59 | if (clearAfter) { 60 | this.clear(); 61 | } 62 | 63 | return currentValue; 64 | } 65 | } 66 | 67 | module.exports = { 68 | Pipeline 69 | }; 70 | -------------------------------------------------------------------------------- /src/pipeline.spec.js: -------------------------------------------------------------------------------- 1 | const { describe, it } = require('mocha'); 2 | const { expect } = require('chai'); 3 | const { Pipeline } = require('./pipeline'); 4 | 5 | const double = x => x * 2; 6 | 7 | describe('Pipeline', () => { 8 | describe('with pipes', () => { 9 | it('should correctly execute all the pushed functions', async () => { 10 | const addAsync = async (x, toAdd) => x + toAdd; 11 | const negateAsync = x => new Promise(resolve => setTimeout(() => resolve(-x), 1)); 12 | const pipeline = new Pipeline(); 13 | const initial = 1; 14 | const expected = -3; 15 | const result = await pipeline.pipe(double, [addAsync, 1], negateAsync).process(initial); 16 | expect(result).to.equal(expected); 17 | }); 18 | }); 19 | 20 | describe('without pipes', () => { 21 | it('should return the initial result when there are no pipes', async () => { 22 | let pipeline = new Pipeline(); 23 | const initial = 1; 24 | const expected = 1; 25 | let result = await pipeline.process(initial); 26 | expect(result).to.equal(expected); 27 | 28 | pipeline = new Pipeline(); 29 | result = await pipeline.pipe(undefined).process(initial); 30 | expect(result).to.equal(expected); 31 | }); 32 | }); 33 | describe('clear pipes', () => { 34 | it('should remove all existing pipes', async () => { 35 | const pipeline = new Pipeline(); 36 | const initial = 1; 37 | let result = await pipeline 38 | .pipe(double) 39 | .clear() 40 | .process(initial); 41 | expect(result).to.equal(initial); 42 | await pipeline.pipe(double).process(initial, true); 43 | result = await pipeline.process(initial); 44 | expect(result).to.equal(initial); 45 | }); 46 | }); 47 | }); 48 | -------------------------------------------------------------------------------- /src/printer.js: -------------------------------------------------------------------------------- 1 | const vscode = require('vscode'); 2 | 3 | const channel = vscode.window.createOutputChannel('PHP Parameter Hint'); 4 | 5 | /** 6 | * Print an error 7 | * @param {string} err 8 | */ 9 | const printError = err => { 10 | channel.appendLine(`${new Date().toLocaleString()} Error: ${err}`); 11 | }; 12 | 13 | module.exports = { 14 | printError 15 | }; 16 | -------------------------------------------------------------------------------- /src/providers/hover.js: -------------------------------------------------------------------------------- 1 | const vscode = require('vscode'); 2 | const { printError } = require('../printer'); 3 | const { getDocRegex, getDefRegex } = require('./regex'); 4 | const { isDefined } = require('../utils'); 5 | 6 | const getArgs = async (editor, line, character, showTypes) => { 7 | let argsDef = []; 8 | let args = []; 9 | const regExDoc = getDocRegex(showTypes); 10 | const regExDef = getDefRegex(showTypes); 11 | 12 | try { 13 | const hoverCommand = await vscode.commands.executeCommand( 14 | 'vscode.executeHoverProvider', 15 | editor.document.uri, 16 | new vscode.Position(line, character) 17 | ); 18 | 19 | if (hoverCommand) { 20 | for (const hover of hoverCommand) { 21 | if (args.length) { 22 | break; 23 | } 24 | 25 | for (const content of hover.contents) { 26 | if (args.length) { 27 | break; 28 | } 29 | 30 | const paramMatches = content.value.match(regExDoc); 31 | 32 | if (Array.isArray(paramMatches) && paramMatches.length) { 33 | args = [ 34 | ...new Set( 35 | paramMatches 36 | .map(label => { 37 | if (!isDefined(label)) { 38 | return label; 39 | } 40 | 41 | if (showTypes === 'disabled' && label.split(' ').length > 1) { 42 | return label 43 | .split(' ')[1] 44 | .replace('`', '') 45 | .trim(); 46 | } 47 | 48 | return label.replace('`', '').trim(); 49 | }) 50 | .filter(label => isDefined(label) && label !== '') 51 | ) 52 | ]; 53 | } 54 | 55 | // If no parameters annotations found, try a regEx that takes the 56 | // parameters from the function definition in hover content 57 | if (!argsDef.length) { 58 | argsDef = [...new Set(content.value.match(regExDef))]; 59 | } 60 | } 61 | } 62 | 63 | if (!args || !args.length) { 64 | args = argsDef; 65 | } 66 | } 67 | } catch (err) { 68 | printError(err); 69 | return []; 70 | } 71 | 72 | return args; 73 | }; 74 | 75 | module.exports = { 76 | getArgs 77 | }; 78 | -------------------------------------------------------------------------------- /src/providers/regex.js: -------------------------------------------------------------------------------- 1 | // Regex to extract param name/type from function definition 2 | const regExDef = /(?<=\(.*)((\.\.\.)?(&)?\$[a-zA-Z0-9_]+)(?=.*\))/gims; 3 | 4 | // Capture the types as well 5 | const regExDefWithTypes = /(?<=\([^(]*)([^,]*(\.\.\.)?(&)?\$[a-zA-Z0-9_]+)(?=.*\))/gims; 6 | 7 | // Regex to extract param name/type from function doc 8 | const regExDoc = /(?<=@param_ )(?:.*?)((\.\.\.)?(&)?\$[a-zA-Z0-9_]+)/gims; 9 | // Capture the types as well 10 | const regExDocWithTypes = /(?<=@param_ )(([^$])+(\.\.\.)?($)?\$[a-zA-Z0-9_]+)/gims; 11 | 12 | const getDocRegex = showTypes => { 13 | if (showTypes === 'disabled') { 14 | return regExDoc; 15 | } 16 | 17 | return regExDocWithTypes; 18 | }; 19 | 20 | const getDefRegex = showTypes => { 21 | if (showTypes === 'disabled') { 22 | return regExDef; 23 | } 24 | 25 | return regExDefWithTypes; 26 | }; 27 | 28 | module.exports = { 29 | getDocRegex, 30 | getDefRegex 31 | }; 32 | -------------------------------------------------------------------------------- /src/providers/regex.spec.js: -------------------------------------------------------------------------------- 1 | const { describe, it } = require('mocha'); 2 | const { expect } = require('chai'); 3 | const { getDocRegex, getDefRegex } = require('./regex'); 4 | 5 | describe('regexp for documentation', () => { 6 | describe('with data types included', () => { 7 | const regExDocTypes = getDocRegex('types'); 8 | 9 | it('should extract the type and name', () => { 10 | const extractedTypeAndNameArr = new RegExp(regExDocTypes.source, 'gims').exec( 11 | '@param_ `\\Models\\User $user`' 12 | ); 13 | expect(extractedTypeAndNameArr).to.have.lengthOf(5); 14 | expect(extractedTypeAndNameArr[1]).to.equal('`\\Models\\User $user'); 15 | }); 16 | it('should return the correct representation when the argument is variadic', () => { 17 | const extractedTypeAndNameArr = new RegExp(regExDocTypes.source, 'gims').exec( 18 | '@param_ `int ...$numbers`' 19 | ); 20 | expect(extractedTypeAndNameArr).to.have.lengthOf(5); 21 | expect(extractedTypeAndNameArr[1]).to.equal('`int ...$numbers'); 22 | }); 23 | it('should extract the correct representation when the argument is passed by reference', () => { 24 | const extractedTypeAndNameArr = new RegExp(regExDocTypes.source, 'gims').exec( 25 | '@param_ `string &$glue`' 26 | ); 27 | expect(extractedTypeAndNameArr).to.have.lengthOf(5); 28 | expect(extractedTypeAndNameArr[1]).to.equal('`string &$glue'); 29 | }); 30 | it('should correctly extract all the params when there are multiple', () => { 31 | const extractedTypeAndNameMatchArr = '@param_ `string $glue` \n @param_ `int ...$numbers`'.match( 32 | new RegExp(regExDocTypes.source, 'gims') 33 | ); 34 | expect(extractedTypeAndNameMatchArr).to.have.lengthOf(2); 35 | expect(extractedTypeAndNameMatchArr[0]).to.equal('`string $glue'); 36 | expect(extractedTypeAndNameMatchArr[1]).to.equal('`int ...$numbers'); 37 | }); 38 | }); 39 | describe('without data types', () => { 40 | const regExDoc = getDocRegex('disabled'); 41 | 42 | it('should extract only the parameter name', () => { 43 | const extractedNameArr = new RegExp(regExDoc.source, 'gims').exec('@param_ `string $glue`'); 44 | expect(extractedNameArr).to.have.lengthOf(4); 45 | expect(extractedNameArr[1]).to.equal('$glue'); 46 | }); 47 | it('should extract the correct representation when the argument is variadic', () => { 48 | const extractedNameArr = new RegExp(regExDoc.source, 'gims').exec( 49 | '@param_ `int ...$numbers`' 50 | ); 51 | expect(extractedNameArr).to.have.lengthOf(4); 52 | expect(extractedNameArr[1]).to.equal('...$numbers'); 53 | }); 54 | it('should extract the correct representation when the argument is passed by reference', () => { 55 | const extractedNameArr = new RegExp(regExDoc.source, 'gims').exec('@param_ `User &$user`'); 56 | expect(extractedNameArr).to.have.lengthOf(4); 57 | expect(extractedNameArr[1]).to.equal('&$user'); 58 | }); 59 | it('should correctly extract all the params when there are multiple', () => { 60 | const extractedNameMatchArr = '@param_ `string $glue` \n @param_ `int ...$numbers`'.match( 61 | new RegExp(regExDoc.source, 'gims') 62 | ); 63 | expect(extractedNameMatchArr).to.have.lengthOf(2); 64 | expect(extractedNameMatchArr[0]).to.equal('`string $glue'); 65 | expect(extractedNameMatchArr[1]).to.equal('`int ...$numbers'); 66 | }); 67 | }); 68 | }); 69 | 70 | describe('regexp for function definition', () => { 71 | describe('with data types included', () => { 72 | const regExDefTypes = getDefRegex('types'); 73 | 74 | it('should extract the type and name', () => { 75 | const extractedTypeAndNameArr = 'function join(string $glue = "", array $pieces)'.match( 76 | new RegExp(regExDefTypes.source, 'gims') 77 | ); 78 | expect(extractedTypeAndNameArr).to.have.lengthOf(2); 79 | expect(extractedTypeAndNameArr[0]).to.equal('string $glue'); 80 | expect(extractedTypeAndNameArr[1]).to.equal(' array $pieces'); 81 | }); 82 | it('should return the correct representation when the argument is variadic', () => { 83 | const extractedTypeAndNameArr = 'function join(int ...$numbers)'.match( 84 | new RegExp(regExDefTypes.source, 'gims') 85 | ); 86 | expect(extractedTypeAndNameArr).to.have.lengthOf(1); 87 | expect(extractedTypeAndNameArr[0]).to.equal('int ...$numbers'); 88 | }); 89 | it('should extract the correct representation when the argument is passed by reference and when there are multiple parameters', () => { 90 | const extractedTypeAndNameArr = 'function join(string &$glue = "", array &$pieces)'.match( 91 | new RegExp(regExDefTypes.source, 'gims') 92 | ); 93 | expect(extractedTypeAndNameArr).to.have.lengthOf(2); 94 | expect(extractedTypeAndNameArr[0]).to.equal('string &$glue'); 95 | expect(extractedTypeAndNameArr[1]).to.equal(' array &$pieces'); 96 | }); 97 | }); 98 | 99 | describe('without data types', () => { 100 | const regExDef = getDefRegex('disabled'); 101 | 102 | it('should extract only the parameter name', () => { 103 | const extractedNameArr = 'function join($glue = "", $pieces)'.match( 104 | new RegExp(regExDef.source, 'gims') 105 | ); 106 | expect(extractedNameArr).to.have.lengthOf(2); 107 | expect(extractedNameArr[0]).to.equal('$glue'); 108 | expect(extractedNameArr[1]).to.equal('$pieces'); 109 | }); 110 | it('should extract the correct representation when the argument is variadic', () => { 111 | const extractedNameArr = 'function join(...$numbers)'.match( 112 | new RegExp(regExDef.source, 'gims') 113 | ); 114 | expect(extractedNameArr).to.have.lengthOf(1); 115 | expect(extractedNameArr[0]).to.equal('...$numbers'); 116 | }); 117 | it('should extract the correct representation when the argument is passed by reference', () => { 118 | const extractedNameArr = 'function join(&$glue)'.match(new RegExp(regExDef.source, 'gims')); 119 | expect(extractedNameArr).to.have.lengthOf(1); 120 | expect(extractedNameArr[0]).to.equal('&$glue'); 121 | }); 122 | }); 123 | }); 124 | -------------------------------------------------------------------------------- /src/providers/signature.js: -------------------------------------------------------------------------------- 1 | const vscode = require('vscode'); 2 | const { printError } = require('../printer'); 3 | const { getDocRegex } = require('./regex'); 4 | 5 | const getArgs = async (editor, line, character, showTypes) => { 6 | let signature; 7 | const signatureHelp = await vscode.commands.executeCommand( 8 | 'vscode.executeSignatureHelpProvider', 9 | editor.document.uri, 10 | new vscode.Position(line, character) 11 | ); 12 | 13 | if (signatureHelp) { 14 | [signature] = signatureHelp.signatures; 15 | } 16 | 17 | if (signature && signature.parameters) { 18 | try { 19 | return signature.parameters.map(parameter => { 20 | const regExDoc = getDocRegex(showTypes); 21 | /** 22 | * If there is a phpDoc for the parameter, use it as the doc 23 | * provides more types 24 | */ 25 | if (parameter.documentation && parameter.documentation.value) { 26 | const docLabel = new RegExp(regExDoc.source, 'gims') 27 | .exec(parameter.documentation.value)[1] 28 | .replace('`', '') 29 | .trim(); 30 | 31 | /** 32 | * Doc wrongfully shows variadic param type as array so we remove it 33 | */ 34 | return docLabel.indexOf('[]') !== -1 && docLabel.indexOf('...') !== -1 35 | ? docLabel.replace('[]', '') 36 | : docLabel; 37 | } 38 | 39 | // Fallback to label 40 | const splittedLabel = parameter.label.split(' '); 41 | 42 | if (showTypes === 'disabled') { 43 | return splittedLabel[0]; 44 | } 45 | 46 | /** 47 | * For cases with default param, like: '$glue = ""', 48 | * take only the param name 49 | */ 50 | return splittedLabel[0].indexOf('$') !== -1 51 | ? splittedLabel[0] 52 | : splittedLabel.slice(0, 2).join(' '); 53 | }); 54 | } catch (err) { 55 | printError(err); 56 | return []; 57 | } 58 | } 59 | 60 | return []; 61 | }; 62 | 63 | module.exports = { 64 | getArgs 65 | }; 66 | -------------------------------------------------------------------------------- /src/update.js: -------------------------------------------------------------------------------- 1 | const vscode = require('vscode'); 2 | // const { singleton } = require('js-coroutines'); 3 | const getHints = require('./parameterExtractor'); 4 | const { printError } = require('./printer'); 5 | const Hints = require('./hints'); 6 | const { pause } = require('./utils'); 7 | 8 | const hintDecorationType = vscode.window.createTextEditorDecorationType({}); 9 | const slowAfterNrParam = 300; 10 | const showParamsOnceEvery = 100; 11 | let runId = 0; 12 | 13 | /** 14 | * The function that creates the new decorations, if the number of arguments 15 | * is bigger than slowAfterNrParam, then the update of the decorations will be 16 | * called once every showParamsOnceEvery 17 | * 18 | * When the function is called, the last call it's interrupted 19 | * 20 | * @param {vscode.TextEditor} activeEditor 21 | * @param {array} functionGroups 22 | */ 23 | async function update(activeEditor, functionGroups) { 24 | runId = Date.now(); 25 | const currentRunId = runId; 26 | const shouldContinue = () => runId === currentRunId; 27 | const argumentsLen = functionGroups.reduce((accumulator, currentGroup) => { 28 | return accumulator + currentGroup.args.length; 29 | }, 0); 30 | let nrArgs = 0; 31 | const phpDecorations = []; 32 | const functionGroupsLen = functionGroups.length; 33 | const functionDictionary = new Map(); 34 | 35 | for (let index = 0; index < functionGroupsLen; index += 1) { 36 | if (!shouldContinue()) { 37 | return null; 38 | } 39 | 40 | const functionGroup = functionGroups[index]; 41 | let hints; 42 | 43 | try { 44 | hints = await getHints(functionDictionary, functionGroup, activeEditor); 45 | } catch (err) { 46 | printError(err); 47 | } 48 | 49 | if (hints && hints.length) { 50 | for (const hint of hints) { 51 | const decorationPHP = Hints.paramHint(hint.text, hint.range); 52 | phpDecorations.push(decorationPHP); 53 | nrArgs += 1; 54 | 55 | if (argumentsLen > slowAfterNrParam) { 56 | if (nrArgs % showParamsOnceEvery === 0) { 57 | activeEditor.setDecorations(hintDecorationType, phpDecorations); 58 | // Continue on next event loop iteration 59 | await pause(10); 60 | 61 | if (!shouldContinue()) { 62 | return null; 63 | } 64 | } 65 | } 66 | } 67 | } 68 | } 69 | 70 | await pause(10); 71 | 72 | if (!shouldContinue()) { 73 | return null; 74 | } 75 | 76 | activeEditor.setDecorations(hintDecorationType, phpDecorations); 77 | return phpDecorations; 78 | } 79 | 80 | module.exports = { 81 | update 82 | }; 83 | -------------------------------------------------------------------------------- /src/utils.js: -------------------------------------------------------------------------------- 1 | const copy = require('fast-copy'); 2 | 3 | const sameNamePlaceholder = '%'; 4 | 5 | /** 6 | * 7 | * @param {any} value 8 | */ 9 | const isDefined = value => typeof value !== 'undefined'; 10 | 11 | /** 12 | * 13 | * @param {string} code 14 | */ 15 | const removeShebang = code => { 16 | const codeArr = code.split('\n'); 17 | 18 | if (codeArr[0].substr(0, 2) === '#!') { 19 | codeArr[0] = ''; 20 | } 21 | 22 | return codeArr.join('\n'); 23 | }; 24 | 25 | const getCopyFunc = () => { 26 | return copy.default; 27 | }; 28 | 29 | /** 30 | * 31 | * @param {number} time in ms 32 | */ 33 | const pause = (time = 0) => new Promise(resolve => setTimeout(resolve, time)); 34 | 35 | module.exports = { 36 | removeShebang, 37 | sameNamePlaceholder, 38 | isDefined, 39 | getCopyFunc, 40 | pause 41 | }; 42 | -------------------------------------------------------------------------------- /src/utils.spec.js: -------------------------------------------------------------------------------- 1 | const { describe, it } = require('mocha'); 2 | const { expect } = require('chai'); 3 | const { isDefined, removeShebang, getCopyFunc } = require('./utils'); 4 | 5 | describe('isDefined', () => { 6 | it('should return a boolean indicating whether the passed argument is defined', () => { 7 | const hint = 'user:'; 8 | expect(isDefined(undefined)).to.be.false; 9 | expect(isDefined(hint)).to.be.true; 10 | }); 11 | }); 12 | 13 | describe('removeShebang', () => { 14 | it('should remove the shebang if it exists', () => { 15 | const withShebang = { 16 | input: `#!\n { 29 | it('should return the default export only when process.env is not "test"', () => { 30 | expect(() => { 31 | const copy = getCopyFunc(); 32 | const values = [1, 2, 3]; 33 | // @ts-ignore 34 | const clonedValues = copy(values); 35 | expect(values).to.deep.equal(clonedValues); 36 | expect(values).to.not.equal(clonedValues); 37 | }).to.not.throw(); 38 | }); 39 | }); 40 | -------------------------------------------------------------------------------- /test/commands.test.js: -------------------------------------------------------------------------------- 1 | const vscode = require('vscode'); 2 | const path = require('path'); 3 | const { describe, it, before, after } = require('mocha'); 4 | const { expect } = require('chai'); 5 | const { sleep, examplesFolderPath } = require('./utils'); 6 | 7 | describe('commands', () => { 8 | before(async () => { 9 | const uri = vscode.Uri.file(path.join(`${examplesFolderPath}general.php`)); 10 | const document = await vscode.workspace.openTextDocument(uri); 11 | await vscode.window.showTextDocument(document); 12 | await sleep(500); // wait for file to be completely functional 13 | }); 14 | after(async () => { 15 | await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); 16 | }); 17 | 18 | describe('toggleTypeName', () => { 19 | after(async () => { 20 | await vscode.workspace.getConfiguration('phpParameterHint').update('hintTypeName', 0, true); 21 | await sleep(1000); 22 | }); 23 | it('should cycle between available options', async () => { 24 | await vscode.workspace.getConfiguration('phpParameterHint').update('hintTypeName', 0, true); 25 | await sleep(1000); 26 | let hintTypeName = vscode.workspace.getConfiguration('phpParameterHint').get('hintTypeName'); 27 | expect(hintTypeName).to.equal(0); 28 | await vscode.commands.executeCommand('phpParameterHint.toggleTypeName'); 29 | await sleep(1000); 30 | hintTypeName = vscode.workspace.getConfiguration('phpParameterHint').get('hintTypeName'); 31 | expect(hintTypeName).to.equal(1); 32 | await vscode.commands.executeCommand('phpParameterHint.toggleTypeName'); 33 | await sleep(1000); 34 | hintTypeName = vscode.workspace.getConfiguration('phpParameterHint').get('hintTypeName'); 35 | expect(hintTypeName).to.equal(2); 36 | await vscode.commands.executeCommand('phpParameterHint.toggleTypeName'); 37 | await sleep(1000); 38 | hintTypeName = vscode.workspace.getConfiguration('phpParameterHint').get('hintTypeName'); 39 | expect(hintTypeName).to.equal(0); 40 | }); 41 | }); 42 | describe('switchable commands', async () => { 43 | const switchableCommandsTable = [ 44 | { 45 | name: 'toggle', 46 | valueName: 'enabled', 47 | default: true 48 | }, 49 | { 50 | name: 'toggleOnChange', 51 | valueName: 'onChange', 52 | default: false 53 | }, 54 | { 55 | name: 'toggleOnSave', 56 | valueName: 'onSave', 57 | default: true 58 | }, 59 | { 60 | name: 'toggleLiterals', 61 | valueName: 'hintOnlyLiterals', 62 | default: false 63 | }, 64 | { 65 | name: 'toggleLine', 66 | valueName: 'hintOnlyLine', 67 | default: false 68 | }, 69 | { 70 | name: 'toggleVisibleRanges', 71 | valueName: 'hintOnlyVisibleRanges', 72 | default: false 73 | }, 74 | { 75 | name: 'toggleCollapse', 76 | valueName: 'collapseHintsWhenEqual', 77 | default: false 78 | }, 79 | { 80 | name: 'toggleCollapseType', 81 | valueName: 'collapseTypeWhenEqual', 82 | default: false 83 | }, 84 | { 85 | name: 'toggleFullType', 86 | valueName: 'showFullType', 87 | default: false 88 | }, 89 | { 90 | name: 'toggleDollarSign', 91 | valueName: 'showDollarSign', 92 | default: false 93 | } 94 | ]; 95 | after(async () => { 96 | for (const command of switchableCommandsTable) { 97 | await vscode.workspace 98 | .getConfiguration('phpParameterHint') 99 | .update(command.valueName, command.default, true); 100 | } 101 | await sleep(1000); 102 | }); 103 | 104 | for (const command of switchableCommandsTable) { 105 | describe(command.name, () => { 106 | it(`should enable/disable ${command.valueName}`, async () => { 107 | await vscode.workspace 108 | .getConfiguration('phpParameterHint') 109 | .update(command.valueName, true, true); 110 | await sleep(1000); 111 | let value = vscode.workspace.getConfiguration('phpParameterHint').get(command.valueName); 112 | expect(value).to.equal(true); 113 | await vscode.commands.executeCommand(`phpParameterHint.${command.name}`); 114 | await sleep(1000); 115 | value = vscode.workspace.getConfiguration('phpParameterHint').get(command.valueName); 116 | expect(value).to.equal(false); 117 | await vscode.commands.executeCommand(`phpParameterHint.${command.name}`); 118 | await sleep(1000); 119 | value = vscode.workspace.getConfiguration('phpParameterHint').get(command.valueName); 120 | expect(value).to.equal(true); 121 | }); 122 | }); 123 | } 124 | }); 125 | }); 126 | -------------------------------------------------------------------------------- /test/examples/User.php: -------------------------------------------------------------------------------- 1 | { 10 | /** @type {vscode.TextEditor} */ 11 | let editor; 12 | 13 | before(async () => { 14 | const uri = vscode.Uri.file(path.join(`${examplesFolderPath}general.php`)); 15 | const document = await vscode.workspace.openTextDocument(uri); 16 | editor = await vscode.window.showTextDocument(document); 17 | await sleep(500); // wait for file to be completely functional 18 | }); 19 | after(async () => { 20 | await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); 21 | }); 22 | 23 | describe('get', () => { 24 | it('should return the correct function groups', async () => { 25 | const functionGroupsFacade = new FunctionGroupsFacade(new CacheService()); 26 | const functionGroups = await functionGroupsFacade.get( 27 | editor.document.uri.toString(), 28 | editor.document.getText() 29 | ); 30 | const expectedFunctionGroups = [ 31 | { 32 | name: '', 33 | args: [ 34 | { 35 | key: 0, 36 | start: { 37 | line: 12, 38 | character: 10 39 | }, 40 | end: { 41 | line: 12, 42 | character: 14 43 | }, 44 | name: '', 45 | kind: 'string' 46 | }, 47 | { 48 | key: 1, 49 | start: { 50 | line: 12, 51 | character: 16 52 | }, 53 | end: { 54 | line: 12, 55 | character: 25 56 | }, 57 | name: '', 58 | kind: 'array' 59 | } 60 | ], 61 | line: 12, 62 | character: 9 63 | }, 64 | { 65 | name: '', 66 | args: [ 67 | { 68 | key: 0, 69 | start: { 70 | line: 13, 71 | character: 9 72 | }, 73 | end: { 74 | line: 13, 75 | character: 10 76 | }, 77 | name: '', 78 | kind: 'number' 79 | }, 80 | { 81 | key: 1, 82 | start: { 83 | line: 13, 84 | character: 12 85 | }, 86 | end: { 87 | line: 13, 88 | character: 13 89 | }, 90 | name: '', 91 | kind: 'number' 92 | }, 93 | { 94 | key: 2, 95 | start: { 96 | line: 13, 97 | character: 15 98 | }, 99 | end: { 100 | line: 13, 101 | character: 16 102 | }, 103 | name: '', 104 | kind: 'number' 105 | } 106 | ], 107 | line: 13, 108 | character: 8 109 | } 110 | ]; 111 | 112 | expect(functionGroups).to.have.lengthOf(2); 113 | expect(functionGroups).to.deep.equal(expectedFunctionGroups); 114 | }); 115 | }); 116 | }); 117 | -------------------------------------------------------------------------------- /test/hints.test.js: -------------------------------------------------------------------------------- 1 | const vscode = require('vscode'); 2 | const path = require('path'); 3 | const { describe, it, before, after } = require('mocha'); 4 | const { expect } = require('chai'); 5 | const sinon = require('sinon'); 6 | const { sleep, examplesFolderPath } = require('./utils'); 7 | const { FunctionGroupsFacade } = require('../src/functionGroupsFacade'); 8 | const { CacheService } = require('../src/cache'); 9 | const getHints = require('../src/parameterExtractor'); 10 | const Hints = require('../src/hints'); 11 | 12 | describe('hints', () => { 13 | /** @type {{text: string, range: vscode.Range}} */ 14 | let hint; 15 | 16 | before(async () => { 17 | const uri = vscode.Uri.file(path.join(`${examplesFolderPath}general.php`)); 18 | const document = await vscode.workspace.openTextDocument(uri); 19 | const editor = await vscode.window.showTextDocument(document); 20 | await sleep(500); // wait for file to be completely functional 21 | const [functionGroup] = await new FunctionGroupsFacade(new CacheService()).get( 22 | editor.document.uri.toString(), 23 | editor.document.getText() 24 | ); 25 | [hint] = await getHints(new Map(), functionGroup, editor); 26 | }); 27 | after(async () => { 28 | sinon.restore(); 29 | await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); 30 | }); 31 | 32 | it('should have the correct range', () => { 33 | const { range } = Hints.paramHint(hint.text, hint.range); 34 | expect(range.start.line).to.equal(hint.range.start.line); 35 | expect(range.start.character).to.equal(hint.range.start.character); 36 | expect(range.end.line).to.equal(hint.range.end.line); 37 | expect(range.end.character).to.equal(hint.range.end.character); 38 | }); 39 | it('should have the correct css props', () => { 40 | const stub = sinon.stub(vscode.workspace, 'getConfiguration'); 41 | const getStub = sinon.stub(); 42 | stub.withArgs('phpParameterHint').returns({ 43 | get: getStub, 44 | has: sinon.fake(), 45 | inspect: sinon.fake(), 46 | update: sinon.fake() 47 | }); 48 | const expectedOpacity = 0.5; 49 | const expectedFontStyle = 'bold'; 50 | const expectedFontWeight = 500; 51 | const expectedFontSize = 13; 52 | const expectedBorderRadius = 6; 53 | const expectedVerticalPadding = 2; 54 | const expectedHorizontalPadding = 5; 55 | const expectedMargin = 3; 56 | const expectedColor = { 57 | id: 'phpParameterHint.hintForeground' 58 | }; 59 | const expectedBackgroundColor = { 60 | id: 'phpParameterHint.hintBackground' 61 | }; 62 | 63 | getStub 64 | .withArgs('opacity') 65 | .returns(expectedOpacity) 66 | .withArgs('fontStyle') 67 | .returns(expectedFontStyle) 68 | .withArgs('fontWeight') 69 | .returns(expectedFontWeight) 70 | .withArgs('fontSize') 71 | .returns(expectedFontSize) 72 | .withArgs('borderRadius') 73 | .returns(expectedBorderRadius) 74 | .withArgs('verticalPadding') 75 | .returns(expectedVerticalPadding) 76 | .withArgs('horizontalPadding') 77 | .returns(expectedHorizontalPadding) 78 | .withArgs('margin') 79 | .returns(expectedMargin); 80 | 81 | const { 82 | renderOptions: { 83 | before: { 84 | opacity, 85 | contentText, 86 | color, 87 | backgroundColor, 88 | margin, 89 | fontStyle, 90 | fontWeight, 91 | borderRadius 92 | } 93 | } 94 | } = Hints.paramHint(hint.text, hint.range); 95 | expect(opacity).to.equal(expectedOpacity); 96 | expect(color).to.deep.equal(expectedColor); 97 | expect(backgroundColor).to.deep.equal(expectedBackgroundColor); 98 | expect(fontStyle).to.equal(expectedFontStyle); 99 | expect(fontWeight).to.equal(`${expectedFontWeight};font-size:${expectedFontSize}px;`); 100 | expect(contentText).to.equal(hint.text); 101 | expect(borderRadius).to.equal(`${expectedBorderRadius}px`); 102 | expect(opacity).to.equal(expectedOpacity); 103 | expect(margin).to.equal( 104 | `0px ${expectedMargin + 105 | 1}px 0px ${expectedMargin}px;padding: ${expectedVerticalPadding}px ${expectedHorizontalPadding}px;` 106 | ); 107 | }); 108 | }); 109 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | const vscode = require('vscode'); 2 | const path = require('path'); 3 | const Mocha = require('mocha'); 4 | const glob = require('glob'); 5 | const { sleep, examplesFolderPath } = require('./utils'); 6 | 7 | function run() { 8 | process.env.NODE_ENV = 'test'; 9 | // Create the mocha test 10 | const mocha = new Mocha({ 11 | ui: 'tdd', 12 | color: true, 13 | timeout: '600s' 14 | }); 15 | 16 | const testsRoot = path.resolve(__dirname, '..'); 17 | 18 | return new Promise((c, e) => { 19 | glob('test/**/**.test.js', { cwd: testsRoot }, (err, files) => { 20 | if (err) { 21 | e(err); 22 | return; 23 | } 24 | 25 | // Add files to the test suite 26 | files.forEach(f => mocha.addFile(path.resolve(testsRoot, f))); 27 | mocha.rootHooks({ 28 | beforeAll: async () => { 29 | const uri = vscode.Uri.file(path.join(`${examplesFolderPath}general.php`)); 30 | const document = await vscode.workspace.openTextDocument(uri); 31 | await vscode.window.showTextDocument(document); 32 | await sleep(4000); // wait for file to be completely functional 33 | } 34 | }); 35 | 36 | try { 37 | // Run the mocha test 38 | mocha.run(failures => { 39 | if (failures > 0) { 40 | e(new Error(`${failures} tests failed.`)); 41 | } else { 42 | c(); 43 | } 44 | }); 45 | } catch (error) { 46 | // eslint-disable-next-line no-console 47 | console.error(error); 48 | e(error); 49 | } 50 | }); 51 | }); 52 | } 53 | 54 | module.exports = { 55 | run 56 | }; 57 | -------------------------------------------------------------------------------- /test/middlewares.test.js: -------------------------------------------------------------------------------- 1 | const vscode = require('vscode'); 2 | const path = require('path'); 3 | const { describe, it, before, afterEach, after } = require('mocha'); 4 | const { expect } = require('chai'); 5 | const { getCopyFunc } = require('../src/utils'); 6 | const { sleep, examplesFolderPath } = require('./utils'); 7 | const { FunctionGroupsFacade } = require('../src/functionGroupsFacade'); 8 | const { CacheService } = require('../src/cache'); 9 | const { onlyLiterals, onlySelection, onlyVisibleRanges } = require('../src/middlewares'); 10 | const { Pipeline } = require('../src/pipeline'); 11 | 12 | const copy = getCopyFunc(); 13 | 14 | describe('middlewares', () => { 15 | /** @type {vscode.TextEditor} */ 16 | let editor; 17 | let initialFunctionGroups; 18 | 19 | before(async () => { 20 | const functionGroupsFacade = new FunctionGroupsFacade(new CacheService()); 21 | const uri = vscode.Uri.file(path.join(`${examplesFolderPath}middlewares.php`)); 22 | const document = await vscode.workspace.openTextDocument(uri); 23 | editor = await vscode.window.showTextDocument(document); 24 | await sleep(500); // wait for file to be completely functional 25 | initialFunctionGroups = await functionGroupsFacade.get( 26 | editor.document.uri.toString(), 27 | editor.document.getText() 28 | ); 29 | }); 30 | 31 | describe('onlyLiterals', () => { 32 | it('should return only function groups and args with literals when called with should apply true', () => { 33 | // @ts-ignore 34 | const onlyLiteralsFunctionGroups = onlyLiterals(copy(initialFunctionGroups), true); 35 | const expectedFunctionGroups = [ 36 | { 37 | name: '', 38 | args: [ 39 | { 40 | key: 0, 41 | start: { 42 | line: 3, 43 | character: 5 44 | }, 45 | end: { 46 | line: 3, 47 | character: 9 48 | }, 49 | name: '', 50 | kind: 'string' 51 | } 52 | ], 53 | line: 3, 54 | character: 4 55 | }, 56 | { 57 | name: '', 58 | args: [ 59 | { 60 | key: 0, 61 | start: { 62 | line: 5, 63 | character: 24 64 | }, 65 | end: { 66 | line: 5, 67 | character: 35 68 | }, 69 | name: '', 70 | kind: 'string' 71 | } 72 | ], 73 | line: 5, 74 | character: 23 75 | }, 76 | { 77 | name: '', 78 | args: [ 79 | { 80 | key: 0, 81 | start: { 82 | line: 98, 83 | character: 32 84 | }, 85 | end: { 86 | line: 98, 87 | character: 45 88 | }, 89 | name: '', 90 | kind: 'string' 91 | } 92 | ], 93 | line: 98, 94 | character: 31 95 | } 96 | ]; 97 | expect(onlyLiteralsFunctionGroups).to.deep.equal(expectedFunctionGroups); 98 | }); 99 | it('should return the same function groups when called with should apply false', () => { 100 | // @ts-ignore 101 | const onlyLiteralsFunctionGroups = onlyLiterals(copy(initialFunctionGroups), false); 102 | expect(initialFunctionGroups).to.deep.equal(onlyLiteralsFunctionGroups); 103 | }); 104 | }); 105 | describe('onlySelection', () => { 106 | before(() => { 107 | const { range } = editor.document.lineAt(5); 108 | editor.selection = new vscode.Selection(range.start, range.end); 109 | editor.revealRange(range); 110 | }); 111 | it('should return hints only for current line/selection when called with should apply true', () => { 112 | // @ts-ignore 113 | const selectionFunctionGroups = onlySelection(copy(initialFunctionGroups), editor, true); 114 | const expectedFunctionGroups = [ 115 | { 116 | name: '', 117 | args: [ 118 | { 119 | key: 0, 120 | start: { 121 | line: 5, 122 | character: 24 123 | }, 124 | end: { 125 | line: 5, 126 | character: 35 127 | }, 128 | name: '', 129 | kind: 'string' 130 | } 131 | ], 132 | line: 5, 133 | character: 23 134 | } 135 | ]; 136 | expect(selectionFunctionGroups).to.deep.equal(expectedFunctionGroups); 137 | }); 138 | it('should return the same function groups when called with should apply false', () => { 139 | // @ts-ignore 140 | const selectionFunctionGroups = onlySelection(copy(initialFunctionGroups), editor, false); 141 | expect(selectionFunctionGroups).to.deep.equal(initialFunctionGroups); 142 | }); 143 | }); 144 | describe('onlyVisibleRanges', () => { 145 | it('should return hints only for visible ranges when called with should apply true', () => { 146 | const visibleRangesFunctionGroups = onlyVisibleRanges( 147 | // @ts-ignore 148 | copy(initialFunctionGroups), 149 | editor, 150 | true 151 | ); 152 | const expectedFunctionGroups = [ 153 | { 154 | name: '', 155 | args: [ 156 | { 157 | key: 0, 158 | start: { 159 | line: 3, 160 | character: 5 161 | }, 162 | end: { 163 | line: 3, 164 | character: 9 165 | }, 166 | name: '', 167 | kind: 'string' 168 | }, 169 | { 170 | key: 1, 171 | start: { 172 | line: 3, 173 | character: 11 174 | }, 175 | end: { 176 | line: 3, 177 | character: 19 178 | }, 179 | name: 'numbers', 180 | kind: 'variable' 181 | } 182 | ], 183 | line: 3, 184 | character: 4 185 | }, 186 | { 187 | name: '', 188 | args: [ 189 | { 190 | key: 0, 191 | start: { 192 | line: 5, 193 | character: 24 194 | }, 195 | end: { 196 | line: 5, 197 | character: 35 198 | }, 199 | name: '', 200 | kind: 'string' 201 | } 202 | ], 203 | line: 5, 204 | character: 23 205 | } 206 | ]; 207 | expect(visibleRangesFunctionGroups).to.deep.equal(expectedFunctionGroups); 208 | }); 209 | it('should return the same function groups when called with should apply false', () => { 210 | const visibleRangesFunctionGroups = onlyVisibleRanges( 211 | // @ts-ignore 212 | copy(initialFunctionGroups), 213 | editor, 214 | false 215 | ); 216 | expect(initialFunctionGroups).to.deep.equal(visibleRangesFunctionGroups); 217 | }); 218 | describe('', () => { 219 | before(async () => { 220 | const lastLine = editor.document.lineCount - 1; 221 | const { range } = editor.document.lineAt(lastLine); 222 | editor.selection = new vscode.Selection(range.start, range.end); 223 | editor.revealRange(range); 224 | await sleep(500); // wait for editor to scroll 225 | }); 226 | after(async () => { 227 | const { range } = editor.document.lineAt(0); 228 | editor.selection = new vscode.Selection(range.start, range.end); 229 | editor.revealRange(range); 230 | await sleep(500); // wait for editor to scroll 231 | }); 232 | 233 | it('should return the new function groups after visible ranges change', async () => { 234 | const visibleRangesFunctionGroups = onlyVisibleRanges( 235 | // @ts-ignore 236 | copy(initialFunctionGroups), 237 | editor, 238 | true 239 | ); 240 | const expectedFunctionGroups = [ 241 | { 242 | name: '', 243 | args: [ 244 | { 245 | key: 0, 246 | start: { 247 | line: 98, 248 | character: 32 249 | }, 250 | end: { 251 | line: 98, 252 | character: 45 253 | }, 254 | name: '', 255 | kind: 'string' 256 | } 257 | ], 258 | line: 98, 259 | character: 31 260 | } 261 | ]; 262 | expect(expectedFunctionGroups).to.deep.equal(visibleRangesFunctionGroups); 263 | }); 264 | }); 265 | }); 266 | describe('combination of middlewares', () => { 267 | const pipeline = new Pipeline(); 268 | 269 | afterEach(() => { 270 | pipeline.clear(); 271 | }); 272 | 273 | it('should return only literals and in visible ranges function groups', async () => { 274 | const onlyLiteralsAndVisibleRangesFunctionGroups = await pipeline 275 | .pipe([onlyLiterals, true], [onlyVisibleRanges, editor, true]) 276 | // @ts-ignore 277 | .process(copy(initialFunctionGroups)); 278 | const expectedFunctionGroups = [ 279 | { 280 | name: '', 281 | args: [ 282 | { 283 | key: 0, 284 | start: { 285 | line: 3, 286 | character: 5 287 | }, 288 | end: { 289 | line: 3, 290 | character: 9 291 | }, 292 | name: '', 293 | kind: 'string' 294 | } 295 | ], 296 | line: 3, 297 | character: 4 298 | }, 299 | { 300 | name: '', 301 | args: [ 302 | { 303 | key: 0, 304 | start: { 305 | line: 5, 306 | character: 24 307 | }, 308 | end: { 309 | line: 5, 310 | character: 35 311 | }, 312 | name: '', 313 | kind: 'string' 314 | } 315 | ], 316 | line: 5, 317 | character: 23 318 | } 319 | ]; 320 | expect(onlyLiteralsAndVisibleRangesFunctionGroups).to.deep.equal(expectedFunctionGroups); 321 | }); 322 | it('should return the same function groups when all middlewares are called with should apply false', async () => { 323 | const onlyLiteralsAndVisibleRangesFunctionGroups = await pipeline 324 | .pipe([onlyLiterals, false], [onlyVisibleRanges, editor, false]) 325 | // @ts-ignore 326 | .process(copy(initialFunctionGroups)); 327 | expect(onlyLiteralsAndVisibleRangesFunctionGroups).to.deep.equal(initialFunctionGroups); 328 | }); 329 | }); 330 | }); 331 | -------------------------------------------------------------------------------- /test/parameterExtractor.test.js: -------------------------------------------------------------------------------- 1 | const vscode = require('vscode'); 2 | const path = require('path'); 3 | const { describe, it, after, before } = require('mocha'); 4 | const { expect } = require('chai'); 5 | const { getCopyFunc } = require('../src/utils'); 6 | const { FunctionGroupsFacade } = require('../src/functionGroupsFacade'); 7 | const { CacheService } = require('../src/cache'); 8 | const getHints = require('../src/parameterExtractor'); 9 | const { sleep, examplesFolderPath } = require('./utils'); 10 | 11 | const copy = getCopyFunc(); 12 | 13 | describe('parameterExtractor', () => { 14 | describe('getHints', () => { 15 | const compareHints = (hints, expectedHints) => { 16 | hints.forEach((hintGroups, indexGroup) => { 17 | hintGroups.forEach((hint, index) => { 18 | expect(hint.text).to.deep.equal(expectedHints[indexGroup][index].text); 19 | expect(hint.range.start.line).to.deep.equal( 20 | expectedHints[indexGroup][index].range.start.line 21 | ); 22 | expect(hint.range.start.character).to.deep.equal( 23 | expectedHints[indexGroup][index].range.start.character 24 | ); 25 | expect(hint.range.end.line).to.deep.equal( 26 | expectedHints[indexGroup][index].range.end.line 27 | ); 28 | expect(hint.range.end.character).to.deep.equal( 29 | expectedHints[indexGroup][index].range.end.character 30 | ); 31 | }); 32 | }); 33 | }; 34 | 35 | describe('general', () => { 36 | let functionGroupsLen; 37 | let functionDictionary; 38 | let expectedNameHints; 39 | const { Range, Position } = vscode; 40 | let editor; 41 | let functionGroups; 42 | 43 | before(async () => { 44 | const functionGroupsFacade = new FunctionGroupsFacade(new CacheService()); 45 | const uri = vscode.Uri.file(path.join(`${examplesFolderPath}general.php`)); 46 | const document = await vscode.workspace.openTextDocument(uri); 47 | editor = await vscode.window.showTextDocument(document); 48 | await sleep(500); // wait for file to fully load 49 | functionGroups = await functionGroupsFacade.get( 50 | editor.document.uri.toString(), 51 | editor.document.getText() 52 | ); 53 | functionGroupsLen = functionGroups.length; 54 | functionDictionary = new Map(); 55 | 56 | expectedNameHints = [ 57 | [ 58 | { 59 | text: 'glue:', 60 | range: new Range(new Position(12, 10), new Position(12, 14)) 61 | }, 62 | { 63 | text: 'pieces:', 64 | range: new Range(new Position(12, 16), new Position(12, 25)) 65 | } 66 | ], 67 | [ 68 | { 69 | text: 'vars[0]:', 70 | range: new Range(new Position(13, 9), new Position(13, 10)) 71 | }, 72 | { 73 | text: 'vars[1]:', 74 | range: new Range(new Position(13, 12), new Position(13, 13)) 75 | }, 76 | { 77 | text: 'vars[2]:', 78 | range: new Range(new Position(13, 15), new Position(13, 16)) 79 | } 80 | ] 81 | ]; 82 | }); 83 | after(async () => { 84 | await vscode.workspace.getConfiguration('phpParameterHint').update('hintTypeName', 0, true); 85 | await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); 86 | await sleep(500); 87 | }); 88 | 89 | const compare = (hints, expectedHints) => { 90 | expect(hints).to.have.lengthOf(2); 91 | expect(hints[0]).to.have.lengthOf(2); 92 | expect(hints[1]).to.have.lengthOf(3); 93 | 94 | compareHints(hints, expectedHints); 95 | }; 96 | 97 | it('should return name hints', async () => { 98 | await vscode.workspace.getConfiguration('phpParameterHint').update('hintTypeName', 0, true); 99 | const hints = []; 100 | 101 | for (let index = 0; index < functionGroupsLen; index += 1) { 102 | const functionGroup = functionGroups[index]; 103 | try { 104 | hints.push(await getHints(functionDictionary, functionGroup, editor)); 105 | // eslint-disable-next-line no-empty 106 | } catch (err) {} 107 | } 108 | 109 | compare(hints, expectedNameHints); 110 | }); 111 | it('should return type and name hints', async () => { 112 | await vscode.workspace.getConfiguration('phpParameterHint').update('hintTypeName', 1, true); 113 | // @ts-ignore 114 | const expectedTypeAndNameHints = copy(expectedNameHints); 115 | expectedTypeAndNameHints[0][0].text = 'string glue:'; 116 | expectedTypeAndNameHints[0][1].text = 'array pieces:'; 117 | expectedTypeAndNameHints[1][0].text = 'int vars[0]:'; 118 | expectedTypeAndNameHints[1][1].text = 'int vars[1]:'; 119 | expectedTypeAndNameHints[1][2].text = 'int vars[2]:'; 120 | const hints = []; 121 | 122 | for (let index = 0; index < functionGroupsLen; index += 1) { 123 | const functionGroup = functionGroups[index]; 124 | try { 125 | hints.push(await getHints(functionDictionary, functionGroup, editor)); 126 | // eslint-disable-next-line no-empty 127 | } catch (err) {} 128 | } 129 | 130 | compare(hints, expectedTypeAndNameHints); 131 | }); 132 | 133 | it('should return type hints', async () => { 134 | await vscode.workspace.getConfiguration('phpParameterHint').update('hintTypeName', 2, true); 135 | // @ts-ignore 136 | const expectedTypeHints = copy(expectedNameHints); 137 | expectedTypeHints[0][0].text = 'string:'; 138 | expectedTypeHints[0][1].text = 'array:'; 139 | expectedTypeHints[1][0].text = 'int[0]:'; 140 | expectedTypeHints[1][1].text = 'int[1]:'; 141 | expectedTypeHints[1][2].text = 'int[2]:'; 142 | const hints = []; 143 | 144 | for (let index = 0; index < functionGroupsLen; index += 1) { 145 | const functionGroup = functionGroups[index]; 146 | try { 147 | hints.push(await getHints(functionDictionary, functionGroup, editor)); 148 | // eslint-disable-next-line no-empty 149 | } catch (err) {} 150 | } 151 | 152 | compare(hints, expectedTypeHints); 153 | }); 154 | }); 155 | describe('showDollarSign', () => { 156 | let functionGroupsLen; 157 | let functionDictionary; 158 | let expectedHints; 159 | const { Range, Position } = vscode; 160 | let editor; 161 | let functionGroups; 162 | 163 | before(async () => { 164 | const functionGroupsFacade = new FunctionGroupsFacade(new CacheService()); 165 | const uri = vscode.Uri.file(path.join(`${examplesFolderPath}showDollarSign.php`)); 166 | const document = await vscode.workspace.openTextDocument(uri); 167 | editor = await vscode.window.showTextDocument(document); 168 | await sleep(500); // wait for file to fully load 169 | functionGroups = await functionGroupsFacade.get( 170 | editor.document.uri.toString(), 171 | editor.document.getText() 172 | ); 173 | functionGroupsLen = functionGroups.length; 174 | functionDictionary = new Map(); 175 | 176 | expectedHints = [ 177 | [ 178 | { 179 | text: 'str:', 180 | range: new Range(new Position(2, 16), new Position(2, 21)) 181 | } 182 | ] 183 | ]; 184 | }); 185 | after(async () => { 186 | await vscode.workspace 187 | .getConfiguration('phpParameterHint') 188 | .update('showDollarSign', false, true); 189 | await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); 190 | await sleep(500); 191 | }); 192 | 193 | const compare = (hints, expected) => { 194 | expect(hints).to.have.lengthOf(1); 195 | expect(hints[0]).to.have.lengthOf(1); 196 | compareHints(hints, expected); 197 | }; 198 | 199 | it('should return hints with parameter name without the dollar sign', async () => { 200 | await vscode.workspace 201 | .getConfiguration('phpParameterHint') 202 | .update('showDollarSign', false, true); 203 | const hints = []; 204 | 205 | for (let index = 0; index < functionGroupsLen; index += 1) { 206 | const functionGroup = functionGroups[index]; 207 | try { 208 | hints.push(await getHints(functionDictionary, functionGroup, editor)); 209 | // eslint-disable-next-line no-empty 210 | } catch (err) {} 211 | } 212 | 213 | compare(hints, expectedHints); 214 | }); 215 | it('should return hints with parameter name with dollar sign', async () => { 216 | await vscode.workspace 217 | .getConfiguration('phpParameterHint') 218 | .update('showDollarSign', true, true); 219 | const hints = []; 220 | // @ts-ignore 221 | const expectedDollarHints = copy(expectedHints); 222 | expectedDollarHints[0][0].text = '$str:'; 223 | 224 | for (let index = 0; index < functionGroupsLen; index += 1) { 225 | const functionGroup = functionGroups[index]; 226 | try { 227 | hints.push(await getHints(functionDictionary, functionGroup, editor)); 228 | // eslint-disable-next-line no-empty 229 | } catch (err) {} 230 | } 231 | 232 | compare(hints, expectedDollarHints); 233 | }); 234 | }); 235 | describe('showFullType', () => { 236 | let functionGroupsLen; 237 | let functionDictionary; 238 | let expectedHints; 239 | const { Range, Position } = vscode; 240 | let editor; 241 | let functionGroups; 242 | 243 | before(async () => { 244 | const functionGroupsFacade = new FunctionGroupsFacade(new CacheService()); 245 | const uri = vscode.Uri.file(path.join(`${examplesFolderPath}showFullType.php`)); 246 | const document = await vscode.workspace.openTextDocument(uri); 247 | editor = await vscode.window.showTextDocument(document); 248 | await vscode.workspace.getConfiguration('phpParameterHint').update('hintTypeName', 2, true); // hint only type 249 | await sleep(500); // wait for file to fully load 250 | functionGroups = await functionGroupsFacade.get( 251 | editor.document.uri.toString(), 252 | editor.document.getText() 253 | ); 254 | functionGroupsLen = functionGroups.length; 255 | functionDictionary = new Map(); 256 | 257 | expectedHints = [ 258 | [ 259 | { 260 | text: 'User:', 261 | range: new Range(new Position(8, 8), new Position(8, 18)) 262 | } 263 | ] 264 | ]; 265 | }); 266 | after(async () => { 267 | await vscode.workspace.getConfiguration('phpParameterHint').update('hintTypeName', 0, true); 268 | await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); 269 | await vscode.workspace 270 | .getConfiguration('phpParameterHint') 271 | .update('showFullType', false, true); 272 | await sleep(500); 273 | }); 274 | 275 | const compare = (hints, expected) => { 276 | expect(hints).to.have.lengthOf(1); 277 | expect(hints[0]).to.have.lengthOf(1); 278 | compareHints(hints, expected); 279 | }; 280 | 281 | it('should return hints with short type', async () => { 282 | await vscode.workspace 283 | .getConfiguration('phpParameterHint') 284 | .update('showFullType', false, true); 285 | const hints = []; 286 | 287 | for (let index = 0; index < functionGroupsLen; index += 1) { 288 | const functionGroup = functionGroups[index]; 289 | try { 290 | hints.push(await getHints(functionDictionary, functionGroup, editor)); 291 | // eslint-disable-next-line no-empty 292 | } catch (err) {} 293 | } 294 | 295 | compare(hints, expectedHints); 296 | }); 297 | it('should return hints with full type', async () => { 298 | await vscode.workspace 299 | .getConfiguration('phpParameterHint') 300 | .update('showFullType', true, true); 301 | const hints = []; 302 | // @ts-ignore 303 | const expectedFullTypeHints = copy(expectedHints); 304 | expectedFullTypeHints[0][0].text = 'Models\\User:'; 305 | 306 | for (let index = 0; index < functionGroupsLen; index += 1) { 307 | const functionGroup = functionGroups[index]; 308 | try { 309 | hints.push(await getHints(functionDictionary, functionGroup, editor)); 310 | // eslint-disable-next-line no-empty 311 | } catch (err) {} 312 | } 313 | 314 | compare(hints, expectedFullTypeHints); 315 | }); 316 | }); 317 | describe('collapseHintsWhenEqual', () => { 318 | let functionGroupsLen; 319 | let functionDictionary; 320 | let expectedHints; 321 | const { Range, Position } = vscode; 322 | let editor; 323 | let functionGroups; 324 | 325 | before(async () => { 326 | const functionGroupsFacade = new FunctionGroupsFacade(new CacheService()); 327 | const uri = vscode.Uri.file(path.join(`${examplesFolderPath}collapseHintsWhenEqual.php`)); 328 | const document = await vscode.workspace.openTextDocument(uri); 329 | editor = await vscode.window.showTextDocument(document); 330 | await vscode.workspace.getConfiguration('phpParameterHint').update('hintTypeName', 0, true); // hint only parameter name 331 | await sleep(500); // wait for file to fully load 332 | functionGroups = await functionGroupsFacade.get( 333 | editor.document.uri.toString(), 334 | editor.document.getText() 335 | ); 336 | functionGroupsLen = functionGroups.length; 337 | functionDictionary = new Map(); 338 | 339 | expectedHints = [ 340 | [ 341 | { 342 | text: 'string:', 343 | range: new Range(new Position(3, 16), new Position(3, 23)) 344 | } 345 | ] 346 | ]; 347 | }); 348 | after(async () => { 349 | await vscode.workspace 350 | .getConfiguration('phpParameterHint') 351 | .update('collapseHintsWhenEqual', false, true); 352 | await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); 353 | await sleep(500); 354 | }); 355 | 356 | const compare = (hints, expected) => { 357 | expect(hints).to.have.lengthOf(1); 358 | expect(hints[0]).to.have.lengthOf(1); 359 | compareHints(hints, expected); 360 | }; 361 | 362 | it('should return the hint', async () => { 363 | await vscode.workspace 364 | .getConfiguration('phpParameterHint') 365 | .update('collapseHintsWhenEqual', false, true); 366 | const hints = []; 367 | 368 | for (let index = 0; index < functionGroupsLen; index += 1) { 369 | const functionGroup = functionGroups[index]; 370 | try { 371 | hints.push(await getHints(functionDictionary, functionGroup, editor)); 372 | // eslint-disable-next-line no-empty 373 | } catch (err) {} 374 | } 375 | 376 | compare(hints, expectedHints); 377 | }); 378 | it('should not return the hint because the var name matches the parameter name', async () => { 379 | await vscode.workspace 380 | .getConfiguration('phpParameterHint') 381 | .update('collapseHintsWhenEqual', true, true); 382 | const hints = []; 383 | 384 | for (let index = 0; index < functionGroupsLen; index += 1) { 385 | const functionGroup = functionGroups[index]; 386 | try { 387 | hints.push(await getHints(functionDictionary, functionGroup, editor)); 388 | // eslint-disable-next-line no-empty 389 | } catch (err) {} 390 | } 391 | 392 | expect(hints).to.have.lengthOf(1); 393 | expect(hints[0]).to.have.lengthOf(0); 394 | }); 395 | }); 396 | describe('collapseTypeWhenEqual', () => { 397 | let functionGroupsLen; 398 | let functionDictionary; 399 | let expectedHints; 400 | const { Range, Position } = vscode; 401 | let editor; 402 | let functionGroups; 403 | 404 | before(async () => { 405 | const functionGroupsFacade = new FunctionGroupsFacade(new CacheService()); 406 | const uri = vscode.Uri.file(path.join(`${examplesFolderPath}collapseTypeWhenEqual.php`)); 407 | const document = await vscode.workspace.openTextDocument(uri); 408 | editor = await vscode.window.showTextDocument(document); 409 | await vscode.workspace.getConfiguration('phpParameterHint').update('hintTypeName', 1, true); // hint type and name 410 | await sleep(500); // wait for file to fully load 411 | functionGroups = await functionGroupsFacade.get( 412 | editor.document.uri.toString(), 413 | editor.document.getText() 414 | ); 415 | functionGroupsLen = functionGroups.length; 416 | functionDictionary = new Map(); 417 | 418 | expectedHints = [ 419 | [ 420 | { 421 | text: 'string string:', 422 | range: new Range(new Position(3, 16), new Position(3, 23)) 423 | } 424 | ] 425 | ]; 426 | }); 427 | after(async () => { 428 | await vscode.workspace.getConfiguration('phpParameterHint').update('hintTypeName', 0, true); // hint type and name 429 | await vscode.workspace 430 | .getConfiguration('phpParameterHint') 431 | .update('collapseTypeWhenEqual', false, true); // hint type and name 432 | await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); 433 | await sleep(500); 434 | }); 435 | 436 | const compare = (hints, expected) => { 437 | expect(hints).to.have.lengthOf(1); 438 | expect(hints[0]).to.have.lengthOf(1); 439 | compareHints(hints, expected); 440 | }; 441 | 442 | it('should return the hint with both type and name', async () => { 443 | await vscode.workspace 444 | .getConfiguration('phpParameterHint') 445 | .update('collapseTypeWhenEqual', false, true); 446 | const hints = []; 447 | 448 | for (let index = 0; index < functionGroupsLen; index += 1) { 449 | const functionGroup = functionGroups[index]; 450 | try { 451 | hints.push(await getHints(functionDictionary, functionGroup, editor)); 452 | // eslint-disable-next-line no-empty 453 | } catch (err) {} 454 | } 455 | 456 | compare(hints, expectedHints); 457 | }); 458 | it('should return the hint only with parameter name because it matches with the type', async () => { 459 | await vscode.workspace 460 | .getConfiguration('phpParameterHint') 461 | .update('collapseTypeWhenEqual', true, true); 462 | const hints = []; 463 | // @ts-ignore 464 | const expectedCollapseTypeHints = copy(expectedHints); 465 | expectedCollapseTypeHints[0][0].text = 'string:'; 466 | 467 | for (let index = 0; index < functionGroupsLen; index += 1) { 468 | const functionGroup = functionGroups[index]; 469 | try { 470 | hints.push(await getHints(functionDictionary, functionGroup, editor)); 471 | // eslint-disable-next-line no-empty 472 | } catch (err) {} 473 | } 474 | 475 | compare(hints, expectedCollapseTypeHints); 476 | }); 477 | }); 478 | }); 479 | }); 480 | -------------------------------------------------------------------------------- /test/providers.test.js: -------------------------------------------------------------------------------- 1 | const vscode = require('vscode'); 2 | const path = require('path'); 3 | const { describe, it, before, after } = require('mocha'); 4 | const { expect } = require('chai'); 5 | const { sleep, examplesFolderPath } = require('./utils'); 6 | const { FunctionGroupsFacade } = require('../src/functionGroupsFacade'); 7 | const { CacheService } = require('../src/cache'); 8 | const signature = require('../src/providers/signature'); 9 | const hover = require('../src/providers/hover'); 10 | 11 | describe('providers', () => { 12 | /** @type {vscode.TextEditor} */ 13 | let editor; 14 | let functionGroups; 15 | 16 | before(async () => { 17 | const functionGroupsFacade = new FunctionGroupsFacade(new CacheService()); 18 | const uri = vscode.Uri.file(path.join(`${examplesFolderPath}providers.php`)); 19 | const document = await vscode.workspace.openTextDocument(uri); 20 | editor = await vscode.window.showTextDocument(document); 21 | await sleep(500); // wait for file to be completely functional 22 | functionGroups = await functionGroupsFacade.get( 23 | editor.document.uri.toString(), 24 | editor.document.getText() 25 | ); 26 | }); 27 | after(async () => { 28 | await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); 29 | }); 30 | 31 | const getArgs = async (provider, showType) => { 32 | /** @type {array} */ 33 | const args = await Promise.all( 34 | functionGroups.map(functionGroup => { 35 | const line = provider === signature ? functionGroup.args[0].start.line : functionGroup.line; 36 | const character = 37 | provider === signature ? functionGroup.args[0].start.character : functionGroup.character; 38 | return provider.getArgs(editor, line, character, showType); 39 | }) 40 | ); 41 | 42 | // @ts-ignore 43 | return args.flat(); 44 | }; 45 | const providers = [ 46 | { 47 | name: 'signature', 48 | func: signature 49 | }, 50 | { 51 | name: 'hover', 52 | func: hover 53 | } 54 | ]; 55 | 56 | for (const provider of providers) { 57 | describe(provider.name, () => { 58 | it('should return only parameters names', async () => { 59 | const args = await getArgs(provider.func, 'disabled'); 60 | const expectedArgs = ['$glue', '$pieces', '$name', '...$ages']; 61 | expect(args).to.deep.equal(expectedArgs); 62 | }); 63 | it('should return parameters names and types', async () => { 64 | let args = await getArgs(provider.func, 'type and name'); 65 | const expectedArgs = ['string $glue', 'array $pieces', 'string $name', 'mixed ...$ages']; 66 | expect(args).to.deep.equal(expectedArgs); 67 | args = await getArgs(provider.func, 'type'); 68 | expect(args).to.deep.equal(expectedArgs); 69 | }); 70 | }); 71 | } 72 | }); 73 | -------------------------------------------------------------------------------- /test/runTest.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable no-console */ 2 | const path = require('path'); 3 | const cp = require('child_process'); 4 | const { 5 | runTests, 6 | resolveCliPathFromVSCodeExecutablePath, 7 | downloadAndUnzipVSCode 8 | } = require('vscode-test'); 9 | 10 | async function main() { 11 | try { 12 | // The folder containing the Extension Manifest package.json 13 | // Passed to `--extensionDevelopmentPath` 14 | const extensionDevelopmentPath = path.resolve(__dirname, '../'); 15 | 16 | // The path to the extension test runner script 17 | // Passed to --extensionTestsPath 18 | const extensionTestsPath = path.resolve(__dirname, './index'); 19 | const vscodeExecutablePath = await downloadAndUnzipVSCode('insiders'); 20 | const cliPath = resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath); 21 | 22 | // Use cp.spawn / cp.exec for custom setup 23 | cp.spawnSync(cliPath, ['--install-extension', 'bmewburn.vscode-intelephense-client'], { 24 | encoding: 'utf-8', 25 | stdio: 'inherit' 26 | }); 27 | 28 | // Download VS Code, unzip it and run the integration test 29 | await runTests({ 30 | vscodeExecutablePath, 31 | extensionDevelopmentPath, 32 | extensionTestsPath 33 | }); 34 | } catch (err) { 35 | console.error(err); 36 | console.error('Failed to run tests'); 37 | process.exit(1); 38 | } 39 | } 40 | 41 | main(); 42 | -------------------------------------------------------------------------------- /test/update.test.js: -------------------------------------------------------------------------------- 1 | const vscode = require('vscode'); 2 | const path = require('path'); 3 | const { describe, it, before, after } = require('mocha'); 4 | const { expect } = require('chai'); 5 | const { sleep, examplesFolderPath } = require('./utils'); 6 | const { FunctionGroupsFacade } = require('../src/functionGroupsFacade'); 7 | const { CacheService } = require('../src/cache'); 8 | const { update } = require('../src/update'); 9 | 10 | describe('update', () => { 11 | /** @type {vscode.TextEditor} */ 12 | let editor; 13 | let functionGroups; 14 | const expectedDecorations = [ 15 | { 16 | range: new vscode.Range(new vscode.Position(12, 10), new vscode.Position(12, 14)), 17 | renderOptions: { 18 | before: { 19 | opacity: 0.4, 20 | color: { 21 | id: 'phpParameterHint.hintForeground' 22 | }, 23 | contentText: 'glue:', 24 | backgroundColor: { 25 | id: 'phpParameterHint.hintBackground' 26 | }, 27 | margin: '0px 3px 0px 2px;padding: 1px 4px;', 28 | borderRadius: '5px', 29 | fontStyle: 'italic', 30 | fontWeight: '400;font-size:12px;' 31 | } 32 | } 33 | }, 34 | { 35 | range: new vscode.Range(new vscode.Position(12, 16), new vscode.Position(12, 25)), 36 | renderOptions: { 37 | before: { 38 | opacity: 0.4, 39 | color: { 40 | id: 'phpParameterHint.hintForeground' 41 | }, 42 | contentText: 'pieces:', 43 | backgroundColor: { 44 | id: 'phpParameterHint.hintBackground' 45 | }, 46 | margin: '0px 3px 0px 2px;padding: 1px 4px;', 47 | borderRadius: '5px', 48 | fontStyle: 'italic', 49 | fontWeight: '400;font-size:12px;' 50 | } 51 | } 52 | }, 53 | { 54 | range: new vscode.Range(new vscode.Position(13, 9), new vscode.Position(13, 10)), 55 | renderOptions: { 56 | before: { 57 | opacity: 0.4, 58 | color: { 59 | id: 'phpParameterHint.hintForeground' 60 | }, 61 | contentText: 'vars[0]:', 62 | backgroundColor: { 63 | id: 'phpParameterHint.hintBackground' 64 | }, 65 | margin: '0px 3px 0px 2px;padding: 1px 4px;', 66 | borderRadius: '5px', 67 | fontStyle: 'italic', 68 | fontWeight: '400;font-size:12px;' 69 | } 70 | } 71 | }, 72 | { 73 | range: new vscode.Range(new vscode.Position(13, 12), new vscode.Position(13, 13)), 74 | renderOptions: { 75 | before: { 76 | opacity: 0.4, 77 | color: { 78 | id: 'phpParameterHint.hintForeground' 79 | }, 80 | contentText: 'vars[1]:', 81 | backgroundColor: { 82 | id: 'phpParameterHint.hintBackground' 83 | }, 84 | margin: '0px 3px 0px 2px;padding: 1px 4px;', 85 | borderRadius: '5px', 86 | fontStyle: 'italic', 87 | fontWeight: '400;font-size:12px;' 88 | } 89 | } 90 | }, 91 | { 92 | range: new vscode.Range(new vscode.Position(13, 15), new vscode.Position(13, 16)), 93 | renderOptions: { 94 | before: { 95 | opacity: 0.4, 96 | color: { 97 | id: 'phpParameterHint.hintForeground' 98 | }, 99 | contentText: 'vars[2]:', 100 | backgroundColor: { 101 | id: 'phpParameterHint.hintBackground' 102 | }, 103 | margin: '0px 3px 0px 2px;padding: 1px 4px;', 104 | borderRadius: '5px', 105 | fontStyle: 'italic', 106 | fontWeight: '400;font-size:12px;' 107 | } 108 | } 109 | } 110 | ]; 111 | 112 | before(async () => { 113 | const uri = vscode.Uri.file(path.join(`${examplesFolderPath}general.php`)); 114 | const document = await vscode.workspace.openTextDocument(uri); 115 | editor = await vscode.window.showTextDocument(document); 116 | await sleep(500); // wait for file to be completely functional 117 | functionGroups = await new FunctionGroupsFacade(new CacheService()).get( 118 | editor.document.uri.toString(), 119 | editor.document.getText() 120 | ); 121 | }); 122 | after(async () => { 123 | await vscode.commands.executeCommand('workbench.action.closeActiveEditor'); 124 | }); 125 | 126 | it('should return the correct decorations', async () => { 127 | const decorations = await update(editor, functionGroups); 128 | expect(decorations).to.deep.equal(expectedDecorations); 129 | }); 130 | it('should cancel the first call and return null when a second one is made and the first one has not finished', async () => { 131 | const [firstDecorations, secondDecorations] = await Promise.all([ 132 | update(editor, functionGroups), 133 | update(editor, functionGroups) 134 | ]); 135 | expect(firstDecorations).to.be.null; 136 | expect(secondDecorations).to.deep.equal(expectedDecorations); 137 | }); 138 | }); 139 | -------------------------------------------------------------------------------- /test/utils.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | const sleep = ms => { 4 | return new Promise(resolve => { 5 | setTimeout(resolve, ms); 6 | }); 7 | }; 8 | 9 | const examplesFolderPath = path.join(`${__dirname}/examples/`); 10 | 11 | module.exports = { 12 | sleep, 13 | examplesFolderPath 14 | }; 15 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | 3 | const path = require('path'); 4 | 5 | /** @type {import('webpack').Configuration} */ 6 | const config = { 7 | target: 'node', 8 | entry: './src/extension.js', 9 | output: { 10 | path: path.resolve(__dirname, 'dist'), 11 | filename: 'extension.js', 12 | libraryTarget: 'commonjs2', 13 | devtoolModuleFilenameTemplate: '../[resource-path]' 14 | }, 15 | devtool: 'source-map', 16 | externals: { 17 | vscode: 'commonjs vscode' 18 | }, 19 | resolve: { 20 | extensions: ['.js'] 21 | } 22 | }; 23 | module.exports = config; 24 | --------------------------------------------------------------------------------