From 33c4b87c19d4090f17a74671727063477a4c0870 Mon Sep 17 00:00:00 2001 From: Raphael Le Minor Date: Tue, 18 Apr 2017 20:29:04 +0200 Subject: [PATCH] fix trigger after quotes --- lib/provider.js | 113 ++++++++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 48 deletions(-) diff --git a/lib/provider.js b/lib/provider.js index 8b302df..175e64b 100644 --- a/lib/provider.js +++ b/lib/provider.js @@ -5,73 +5,90 @@ import fuzzy from 'fuzzy'; import nodepath from 'path'; import * as RE from './regularExpression'; -const getFilePath = fullFilePath => fullFilePath.match(RE.withoutFilename)[0]; +class Provider { + constructor() { -const getRequestPath = (line) => { - /* - * match returns an array, - * just get the first value in the array - * with the `'` - */ - const [path] = line.match(RE.insideQuotes); + // autocomplete+ + this.selector = '.source.js'; + this.inclusionPriority = 1; + this.suggestionPriority = 2; + this.excludeLowerPriority = false; + } - const withoutQuotes = path.slice(1, path.length - 1); + getFilePath = fullFilePath => fullFilePath.match(RE.withoutFilename)[0]; - return withoutQuotes.match(RE.withoutFilename)[0]; -} + getRequestPath = (line) => { + /* + * match returns an array, + * just get the first value in the array + * with the `'` + */ + const [path] = line.match(RE.insideQuotes); -const getRequestString = (line) => { - const request = line.match(RE.onlyFilename); - return request.slice(1, request.length - 1); -} + const withoutQuotes = path.slice(1, path.length - 1); -const getType = (stat) => { - if (stat.isFile()) return 'file'; - if (stat.isDirectory()) return 'directory'; - if (stat.isBlockDevice()) return 'block device'; - if (stat.isCharacterDevice()) return 'character device'; - if (stat.isSymbolicLink()) return 'symbolic link'; - if (stat.isFIFO()) return 'FIFO'; - if (stat.isSocket()) return 'socket'; -} + return withoutQuotes.match(RE.withoutFilename)[0]; + } -const formatResult = path => file => { - const separator = path[path.length - 1] === '/' ? '' : '/'; + getRequestString = (line) => { + const request = line.match(RE.onlyFilename)[0]; + return request.slice(1, request.length - 1); + } - const stat = fs.lstatSync(`${path}${separator}${file}`); - const type = getType(stat); + getType = (stat) => { + if (stat.isFile()) return 'file'; + if (stat.isDirectory()) return 'directory'; + if (stat.isBlockDevice()) return 'block device'; + if (stat.isCharacterDevice()) return 'character device'; + if (stat.isSymbolicLink()) return 'symbolic link'; + if (stat.isFIFO()) return 'FIFO'; + if (stat.isSocket()) return 'socket'; + } - return { - // remove the extension - text: nodepath.basename(file, nodepath.extname(file)), - displayText: file, - rightLabelHTML: type, - }; -} + formatResult = path => ({ string: file }) => { + const separator = path[path.length - 1] === '/' ? '' : '/'; -class Provider { - constructor() { + const stat = fs.lstatSync(`${path}${separator}${file}`); + const type = this.getType(stat); - // autocomplete+ - this.selector = '.source.js'; - this.inclusionPriority = 1; - this.suggestionPriority = 2; - this.excludeLowerPriority = false; + return { + // remove the extension + text: nodepath.basename(file, nodepath.extname(file)), + displayText: file, + rightLabelHTML: type, + }; + } + + isQuote = (line, i) => ( + (line[i] === '\'' || line[i] === '\"') && + i - 1 >= 0 && + line[i - 1] !== '\\' + ) + + surrounded = (line, index) => { + let inside = false; + + for (let i = 0; i < index; i++) { + if (this.isQuote(line, i)) { + inside = !inside; + } + } + return inside; } - getSuggestions({ editor, bufferPosition: { row } }) { + getSuggestions({ editor, bufferPosition: { row, column }, prefix }) { return new Promise((resolve, reject) => { const { lines, file: { path: fullFilePath } } = editor.getBuffer(); const line = lines[row]; if (!line.match(RE.importFrom)) resolve([]) - else { - const requestPath = getRequestPath(line); + else if (this.surrounded(line, column)) { + const requestPath = this.getRequestPath(line); - const requestString = getRequestString(line); + const requestString = this.getRequestString(line); - const filePath = getFilePath(fullFilePath); + const filePath = this.getFilePath(fullFilePath); const separator = filePath[filePath.length - 1] === '/' ? '' : '/'; @@ -87,7 +104,7 @@ class Provider { const results = fuzzy.filter(requestString, files); // use fuzzy to get wider results - const suggestions = results.map(formatResult(searchPath)); + const suggestions = results.map(this.formatResult(searchPath)); resolve(suggestions); })