From 8f42dd76a06490c6727e9d393ee02e102d4ea984 Mon Sep 17 00:00:00 2001 From: salexdv Date: Sat, 14 Nov 2020 11:29:32 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A4=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B2=D1=81=D0=B5=D1=85=20=D0=B8=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bsl_helper.js | 99 +++++++++++++++++++++++++++-------------------- src/editor.js | 7 ++++ 2 files changed, 64 insertions(+), 42 deletions(-) diff --git a/src/bsl_helper.js b/src/bsl_helper.js index 8f1ac39..dc768c0 100644 --- a/src/bsl_helper.js +++ b/src/bsl_helper.js @@ -1207,7 +1207,52 @@ class bslHelper { } - + /** + * Fills and returns array of variables names + * + * @param {int} currentLine the last line below which we don't search variables + * + * @returns {array} array with variables names + */ + getVarsNames(currentLine) { + + let names = []; + + const matches = this.model.findMatches('([a-zA-Z0-9\u0410-\u044F_]+)\\s?=\\s?.*(?:;|\\()\\s*$', true, true, false, null, true); + + for (let idx = 0; idx < matches.length; idx++) { + + let match = matches[idx]; + + if (match.range.startLineNumber < currentLine) { + + let varName = match.matches[match.matches.length - 1] + + if (!names.some(name => name === varName)) + names.push(varName); + + } + + } + + const funcDef = editor.getModel().findPreviousMatch('(?:процедура|функция)\\s+[a-zA-Z0-9\u0410-\u044F_]+\\(([a-zA-Z0-9\u0410-\u044F_,\\s=]+)\\)', true, true, false, null, true); + + if (funcDef && 1 < funcDef.matches.length) { + + const params = funcDef.matches[1].split(','); + + params.forEach(function(param) { + let paramName = param.split('=')[0].trim(); + if (!names.some(name => name === paramName)) + names.push(paramName); + }); + + } + + return names; + + } + /** * Fills array of completition for variables * @@ -1217,50 +1262,20 @@ class bslHelper { if (this.word) { - const matches = this.model.findMatches('([a-zA-Z0-9\u0410-\u044F_]+)\\s?=\\s?.*(?:;|\\()\\s*$', true, true, false, null, true); + let names = this.getVarsNames(this.lineNumber); - for (let idx = 0; idx < matches.length; idx++) { + for (let idx = 0; idx < names.length; idx++) { - let match = matches[idx]; + let varName = names[idx]; - if (match.range.startLineNumber < this.lineNumber) { - - let varName = match.matches[match.matches.length - 1]; - - if (varName.toLowerCase().startsWith(this.word)) { - suggestions.push({ - label: varName, - kind: monaco.languages.CompletionItemKind.Variable, - insertText: varName, - insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet - }); - } - - } - - } - - const funcDef = editor.getModel().findPreviousMatch('(?:процедура|функция)\\s+[a-zA-Z0-9\u0410-\u044F_]+\\(([a-zA-Z0-9\u0410-\u044F_,\\s=]+)\\)', true, true, false, null, true); - - if (funcDef && 1 < funcDef.matches.length) { - - const params = funcDef.matches[1].split(','); - let word = this.word; - - params.forEach(function(param){ - - let paramName = param.split('=')[0].trim(); - - if (paramName.toLowerCase().startsWith(word)) { - suggestions.push({ - label: paramName, - kind: monaco.languages.CompletionItemKind.Variable, - insertText: paramName, - insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet - }); - } - - }); + if (varName.toLowerCase().startsWith(this.word)) { + suggestions.push({ + label: varName, + kind: monaco.languages.CompletionItemKind.Variable, + insertText: varName, + insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet + }); + } } diff --git a/src/editor.js b/src/editor.js index af53d97..5fb616c 100644 --- a/src/editor.js +++ b/src/editor.js @@ -271,6 +271,13 @@ define(['bslGlobals', 'bslMetadata', 'snippets', 'bsl_language', 'vs/editor/edit } + getVarsNames = function () { + + let bsl = new bslHelper(editor.getModel(), editor.getPosition()); + return bsl.getVarsNames(editor.getModel().getLineCount()); + + } + editor = undefined; // Register languages