From 3489b513c90117b7f62d11f5012c4b1888ca8f5c Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 29 Aug 2022 23:26:23 +0300 Subject: [PATCH] [autocomplete] find LESS vars --- edit/autocomplete.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/edit/autocomplete.js b/edit/autocomplete.js index 6a24a32e..6f3fa39c 100644 --- a/edit/autocomplete.js +++ b/edit/autocomplete.js @@ -41,6 +41,7 @@ const {line, ch} = pos; const {styles, text} = cm.getLineHandle(line); const {style, index} = cm.getStyleAtPos({styles, pos: ch}) || {}; + const isLessLang = cm.doc.mode.helperType === 'less'; const isStylusLang = cm.doc.mode.name === 'stylus'; const type = style && style.split(' ', 1)[0] || 'prop?'; if (!type || type === 'comment' || type === 'string') { @@ -86,6 +87,7 @@ '@supports', '@viewport', ]; + if (isLessLang) list = findAllCssVars(cm, left, '\\s*:').concat(list); break; case '#': // prevents autocomplete for #hex colors @@ -196,13 +198,15 @@ !style.startsWith(USO_VALID_VAR) && !style.startsWith(USO_INVALID_VAR); } - function findAllCssVars(cm, leftPart) { + function findAllCssVars(cm, leftPart, rightPart = '') { // simplified regex without CSS escapes + const [, prefixed, named] = leftPart.match(/^(--|@)?(\S)?/); const rx = new RegExp( '(?:^|[\\s/;{])(' + - (leftPart.startsWith('--') ? leftPart : '--') + - (leftPart.length <= 2 ? '[a-zA-Z_\u0080-\uFFFF]' : '') + - '[-0-9a-zA-Z_\u0080-\uFFFF]*)', + (prefixed ? leftPart : '--') + + (named ? '' : '[a-zA-Z_\u0080-\uFFFF]') + + '[-0-9a-zA-Z_\u0080-\uFFFF]*)' + + rightPart, 'g'); const list = new Set(); cm.eachLine(({text}) => {