From d3c043a218ca5e8bbb10eeafbd4548757e14f3f8 Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 22 Feb 2022 18:31:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8F=90=E7=A4=BA=E5=88=A4?= =?UTF-8?q?=E6=96=AD;=E8=BF=87=E6=BB=A4`.*`=E5=BC=80=E5=A4=B4=E7=9A=84?= =?UTF-8?q?=E6=96=87=E4=BB=B6;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++++ index.js | 45 ++++++++++++++++++++++++++++++++++++++++----- package.json | 2 +- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61232e8..48faf6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # 更新日志(Changed Logs) +### v1.1.0 +- 优化提示判断, 当前输入需要在被引号包含中 +- 过滤`.*`开头的文件 + ### v1.0.1 - 修复对json文件的支持 diff --git a/index.js b/index.js index fd72b11..06b30cd 100644 --- a/index.js +++ b/index.js @@ -36,7 +36,7 @@ function isfile(path) { function ls(dir) { try { var list = fs.readdirSync(dir) - return list.map(it => resolve(dir, it)) + return list.filter(it => !it.startsWith('.')).map(it => resolve(dir, it)) } catch (err) { return [] } @@ -45,7 +45,18 @@ function ls(dir) { function getPrefixTxt(line, idx) { var txt = line.slice(0, idx) var n = txt.lastIndexOf('"') > -1 ? txt.lastIndexOf('"') : txt.lastIndexOf("'") - return txt.slice(n + 1) + var r + + txt = txt.slice(n + 1) + if (txt) { + // 判断匹配前缀是否被包含在引号中 + r = new RegExp(`(['"])${txt}\\1`) + + if (r.test(line)) { + return txt + } + } + return '' } function item(text, type, p) { @@ -65,10 +76,33 @@ class AutoPath { var inputTxt = doc.lineAt(pos).text // 获取光标所在的整行代码 var list = [] var currDirFixed = '' + var inputTxtTrim = inputTxt.trim() + + // console.log('原始inputTxt >>>> ', inputTxt) + + /** + * 过滤掉 以下几种情况 + * @1 在注释后面的 + * @2 当前光标在行末的 + * @3 匹配前缀在行首的 + */ + if ( + inputTxtTrim.startsWith('// ') || + inputTxtTrim.startsWith('/* ') || + inputTxtTrim.startsWith('# ') || + inputTxtTrim.startsWith('./') || + inputTxtTrim.startsWith('../') || + inputTxtTrim.startsWith('/') || + inputTxtTrim.length === pos.character + ) { + return + } inputTxt = getPrefixTxt(inputTxt, pos.character) currDirFixed = join(currDir, inputTxt) + // console.log('修正后的inputTxt: ', inputTxt) + if (!inputTxt) { return } @@ -111,9 +145,10 @@ class AutoPath { return item(k, t, pos) }) - list.unshift(item('', FILE, pos)) - - return Promise.resolve(list) + if (list.length) { + list.unshift(item('', FILE, pos)) + return Promise.resolve(list) + } } } diff --git a/package.json b/package.json index 9b182b5..01f777b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "auto-path", "displayName": "auto-path", "description": "🔥 自动提示文件路径, 方便引入项目为的文件", - "version": "1.0.2", + "version": "1.1.0", "publisher": "yutent", "author": "Yutent [@yutent]", "icon": "logo.png",