Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rle-mino committed Mar 19, 2018
1 parent 1668895 commit 2c4cbfe
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 199 deletions.
98 changes: 43 additions & 55 deletions lib/provider.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
'use babel';
import * as fs from 'fs';
import * as fuzzy from 'fuzzy';
import * as nodepath from 'path';
import * as path from 'path';
import * as RE from './regularExpression';
class Provider {
constructor() {
this.getActualFilePath = (fullFilePath) => fullFilePath.match(RE.withoutFilename)[0];
this.getRequestPath = (line) => {
const [path] = line.match(RE.insideQuotes);
const withoutQuotes = path.slice(1, path.length - 1);
return withoutQuotes.match(RE.withoutFilename)[0];
};
this.getRequestString = (line) => {
const request = line.match(RE.onlyFilename)[0];
return request.slice(1, request.length - 1);
};
this.getType = (stat) => {
if (stat.isFile())
return 'file';
Expand All @@ -31,25 +21,23 @@ class Provider {
if (stat.isSocket())
return 'socket';
};
this.removeExtension = (extension, actualFileExtension) => extension === actualFileExtension;
this.formatResult = (path, actualFileExtension) => ({ string: file }) => {
const separator = path[path.length - 1] === '/' ? '' : '/';
const stat = fs.lstatSync(`${path}${separator}${file}`);
this.isTheExtensionRemovable = (extension, actualFileExtension) => extension === actualFileExtension;
this.formatResult = (dirname, actualFileExtension) => ({ string: file }) => {
const stat = fs.lstatSync(path.resolve(dirname, file));
const type = this.getType(stat);
const extension = nodepath.extname(file);
const text = this.removeExtension(extension, actualFileExtension) ?
nodepath.basename(file, nodepath.extname(file))
:
file;
const extension = path.extname(file);
const text = this.isTheExtensionRemovable(extension, actualFileExtension)
? path.basename(file, path.extname(file))
: file;
return {
text,
displayText: file,
rightLabelHTML: type,
};
};
this.isQuote = (line, index) => ((line[index] === '\'' || line[index] === '\"') &&
index - 1 >= 0 &&
line[index - 1] !== '\\');
this.isQuote = (line, index) => ((line[index] === '\'' || line[index] === '\"')
&& index - 1 >= 0
&& line[index - 1] !== '\\');
this.isSurrounded = (line, index) => {
let inside = false;
for (let i = 0; i < index; i = i + 1) {
Expand All @@ -59,38 +47,38 @@ class Provider {
}
return inside;
};
this.getSuggestions = ({ editor, bufferPosition: { row, column } }) => {
return new Promise((resolve, reject) => {
const { lines, file: { path: fullFilePath }, } = editor.getBuffer();
const line = lines[row];
if (!line.match(RE.everywhere)) {
resolve([]);
}
else if (this.isSurrounded(line, column)) {
const path = this.getRequestPath(line);
const filename = this.getRequestString(line);
const actualFilePath = this.getActualFilePath(fullFilePath);
const actualFileExtension = nodepath.extname(fullFilePath);
const separator = actualFilePath[actualFilePath.length - 1] === '/' ? '' : '/';
const searchPath = path[0] === '/' ?
path
:
`${actualFilePath}${separator}${path}`;
fs.readdir(searchPath, (err, files) => {
if (err || !files) {
return resolve([]);
}
let validFiles = files;
if (filename[0] !== '.') {
validFiles = files.filter(file => file[0] !== '.');
}
const results = fuzzy.filter(filename, validFiles);
const suggestions = results.map(this.formatResult(searchPath, actualFileExtension));
resolve(suggestions);
});
}
});
};
this.getSuggestions = ({ editor, bufferPosition }) => new Promise((resolve, reject) => {
const { row, column } = bufferPosition;
const lines = editor.buffer.getLines();
const line = lines[row];
const fullFilePath = editor.buffer.file.path;
if (!line.match(RE.looksLikeAnImportStatement)) {
return resolve([]);
}
else if (this.isSurrounded(line, column)) {
const [textInsideQuotes] = line.match(RE.insideQuotes);
const textWithoutQuotes = textInsideQuotes.slice(1, textInsideQuotes.length - 1);
const dirname = path.dirname(textWithoutQuotes);
const filename = path.basename(textWithoutQuotes);
const actualFileDir = path.dirname(fullFilePath);
const actualFileExtension = path.extname(fullFilePath);
const searchPath = dirname[0] === '/'
? dirname
: path.resolve(actualFileDir, dirname);
fs.readdir(searchPath, (err, files) => {
if (err || !files || files.length === 0) {
return resolve([]);
}
let validFiles = files;
if (filename[0] !== '.') {
validFiles = files.filter(file => file[0] !== '.');
}
const results = fuzzy.filter(filename, validFiles);
const suggestions = results.map(this.formatResult(searchPath, actualFileExtension));
resolve(suggestions);
});
}
});
this.selector = '*';
this.inclusionPriority = 1;
this.suggestionPriority = 2;
Expand Down
7 changes: 1 addition & 6 deletions lib/regularExpression.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
'use babel';
export const everywhere = /['"]((\.\/)|(\.\.\/)|(\/))/;
export const withoutFilename = /(.*\/)/;
export const looksLikeAnImportStatement = /['"]((\.\/)|(\.\.\/)|(\/))/;
export const insideQuotes = /['"]((\.\/)|(\.\.\/)|(\/)).*['"]/;
export const onlyFilename = /\/[^/]*['"]/;
export const importFrom = /from\s.*['"]((.\/)|(..\/)|(\/))/;
export const requ = /require\(['"]((\.\/)|(\.\.\/)|(\/)).*/;
export const importO = /import.*['"]((\.\/)|(\/)|(\.\.\/))/;
58 changes: 29 additions & 29 deletions spec/pathr-core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ import provider from '../lib/provider';
// or `fdescribe`). Remove the `f` to unfocus the block.

describe('core functions', () => {
describe('getActualFilePath', () => {
it('should remove filename from a path', () => {
expect(provider.getActualFilePath('../foo/bar/foo/bar.js'))
.toEqual('../foo/bar/foo/');
});
});

describe('getRequestPath', () => {
it('should getRequestPath', () => {
expect(provider.getRequestPath('import a from \'./foo/bar/ok.ts\''))
.toEqual('./foo/bar/');
});
});
// describe('getActualFilePath', () => {
// it('should remove filename from a path', () => {
// expect(provider.getActualFilePath('../foo/bar/foo/bar.js'))
// .toEqual('../foo/bar/foo/');
// });
// });

describe('getRequestString', () => {
it('should getRequestString', () => {
expect(provider.getRequestString('import a from \'./foo/bar.ts\''))
.toEqual('bar.ts');
});
});
// describe('getRequestPath', () => {
// it('should getRequestPath', () => {
// expect(provider.getRequestPath('import a from \'./foo/bar/ok.ts\''))
// .toEqual('./foo/bar/');
// });
// });

describe('removeExtension', () => {
it('should return true if both extensions are the same', () => {
expect(provider.removeExtension('.ts', '.ts'))
.toEqual(true);
});
// describe('getRequestString', () => {
// it('should getRequestString', () => {
// expect(provider.getRequestString('import a from \'./foo/bar.ts\''))
// .toEqual('bar.ts');
// });
// });

it('should return false if extensions are different', () => {
expect(provider.removeExtension('.js', '.ts'))
.toEqual(false);
});
});
// describe('removeExtension', () => {
// it('should return true if both extensions are the same', () => {
// expect(provider.removeExtension('.ts', '.ts'))
// .toEqual(true);
// });
//
// it('should return false if extensions are different', () => {
// expect(provider.removeExtension('.js', '.ts'))
// .toEqual(false);
// });
// });

describe('formatResult', () => {
it('should return the autocomplete+ format object of a file', () => {
Expand Down
36 changes: 16 additions & 20 deletions spec/pathr-spec.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ describe('Pathr', () => {
it('should return an empty array if it is not a known pattern', (done) => {
const params = {
editor: {
getBuffer: () => ({
lines: [
buffer: {
getLines: () => [
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod',
'Lorem ipsum dolor consectetur adipisicing elit, sed do eiusmod sit amet',
],
file: {
path: actual,
},
}),
},
},
bufferPosition: {
row: 0,
Expand All @@ -39,14 +39,14 @@ describe('Pathr', () => {
it('from \'./', (done) => {
const params = {
editor: {
getBuffer: () => ({
lines: [
buffer: {
getLines: () => [
'import a from \'./\'',
],
file: {
path: actual,
},
}),
},
},
bufferPosition: {
row: 0,
Expand All @@ -65,11 +65,6 @@ describe('Pathr', () => {
displayText: 'pathr-spec.test.js',
rightLabelHTML: 'file',
text: 'pathr-spec.test',
},
{
displayText: 'pathr-view-spec.js',
rightLabelHTML: 'file',
text: 'pathr-view-spec',
}],
);
done();
Expand All @@ -79,14 +74,14 @@ describe('Pathr', () => {
it('from \'../', (done) => {
const params = {
editor: {
getBuffer: () => ({
lines: [
buffer: {
getLines: () => [
'import a from \'../\'',
],
file: {
path: actual,
},
}),
},
},
bufferPosition: {
row: 0,
Expand All @@ -103,8 +98,8 @@ describe('Pathr', () => {
it('from \'/', (done) => {
const params = {
editor: {
getBuffer: () => ({
lines: [
buffer: ({
getLines: () => [
'import a from \'/\'',
],
file: {
Expand All @@ -127,14 +122,14 @@ describe('Pathr', () => {
it('should work even if the folder does not exists', (done) => {
const params = {
editor: {
getBuffer: () => ({
lines: [
buffer: {
getLines: () => [
'import a from \'./foo/bar\'',
],
file: {
path: actual,
},
}),
},
},
bufferPosition: {
row: 0,
Expand All @@ -145,6 +140,7 @@ describe('Pathr', () => {
.then((results) => {
expect(results.length).toEqual(0);
done();
});
})
.catch(console.log);
});
});
Loading

0 comments on commit 2c4cbfe

Please sign in to comment.