|
|
|
@ -1,9 +1,18 @@ |
|
|
|
|
import { EOL } from 'os' |
|
|
|
|
import { SearchResultLineLine } from '../../types' |
|
|
|
|
import isElectron from 'is-electron' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getDirectory = async (dir: string, plugin: any) => { |
|
|
|
|
let result = [] |
|
|
|
|
|
|
|
|
|
if (isElectron()) { |
|
|
|
|
|
|
|
|
|
const files = await plugin.call('fs', 'glob', dir, '**/*') |
|
|
|
|
// only get path property of files
|
|
|
|
|
result = files.map(x => x.path) |
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
const files = await plugin.call('fileManager', 'readdir', dir) |
|
|
|
|
const fileArray = normalize(files) |
|
|
|
|
for (const fi of fileArray) { |
|
|
|
@ -16,10 +25,11 @@ export const getDirectory = async (dir: string, plugin: any) => { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return result |
|
|
|
|
} |
|
|
|
|
return result |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const normalize = filesList => { |
|
|
|
|
const normalize = filesList => { |
|
|
|
|
const folders = [] |
|
|
|
|
const files = [] |
|
|
|
|
Object.keys(filesList || {}).forEach(key => { |
|
|
|
@ -36,7 +46,7 @@ export const getDirectory = async (dir: string, plugin: any) => { |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
return [...folders, ...files] |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const findLinesInStringWithMatch = (str: string, re: RegExp) => { |
|
|
|
|
return str |
|
|
|
@ -54,7 +64,7 @@ export const findLinesInStringWithMatch = (str: string, re: RegExp) => { |
|
|
|
|
|
|
|
|
|
const matchesInString = (str: string, re: RegExp) => { |
|
|
|
|
let a: RegExpExecArray |
|
|
|
|
const results:RegExpExecArray[] = []; |
|
|
|
|
const results: RegExpExecArray[] = []; |
|
|
|
|
while ((a = re.exec(str || '')) !== null) { |
|
|
|
|
results.push(a); |
|
|
|
|
} |
|
|
|
@ -63,11 +73,11 @@ const matchesInString = (str: string, re: RegExp) => { |
|
|
|
|
|
|
|
|
|
const splitLines = (matchResult: RegExpExecArray[], lineNumber: number) => { |
|
|
|
|
return matchResult.map((matchResultPart, i) => { |
|
|
|
|
const result:SearchResultLineLine = { |
|
|
|
|
const result: SearchResultLineLine = { |
|
|
|
|
left: matchResultPart.input.substring(0, matchResultPart.index), |
|
|
|
|
right: matchResultPart.input.substring(matchResultPart.index + matchResultPart[0].length), |
|
|
|
|
center: matchResultPart[0], |
|
|
|
|
position : { |
|
|
|
|
position: { |
|
|
|
|
start: { |
|
|
|
|
line: lineNumber, |
|
|
|
|
column: matchResultPart.index, |
|
|
|
@ -92,7 +102,7 @@ function getEOL(text) { |
|
|
|
|
return u > w ? '\n' : '\r\n'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const replaceAllInFile = (string: string, re:RegExp, newText: string) => { |
|
|
|
|
export const replaceAllInFile = (string: string, re: RegExp, newText: string) => { |
|
|
|
|
return string.replace(re, newText) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|