Skip to content

Commit

Permalink
fix trigger after quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Le Minor authored and Raphael Le Minor committed Apr 18, 2017
1 parent 367bba1 commit 33c4b87
Showing 1 changed file with 65 additions and 48 deletions.
113 changes: 65 additions & 48 deletions lib/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] === '/' ? '' : '/';

Expand All @@ -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);
})
Expand Down

0 comments on commit 33c4b87

Please sign in to comment.