|
|
@ -1,28 +1,38 @@ |
|
|
|
import React, { useEffect } from 'react' |
|
|
|
import React, { useEffect } from 'react' |
|
|
|
import { createContext, useReducer } from 'react' |
|
|
|
import { createContext, useReducer } from 'react' |
|
|
|
import { findLinesInStringWithMatch, getDirectory, replaceTextInLine } from '../components/results/SearchHelper' |
|
|
|
|
|
|
|
import { |
|
|
|
import { |
|
|
|
SearchReducer, |
|
|
|
findLinesInStringWithMatch, |
|
|
|
} from '../reducers/Reducer' |
|
|
|
getDirectory, |
|
|
|
import { SearchState, SearchResult, SearchResultLine, SearchResultLineLine, SearchingInitialState } from '../types' |
|
|
|
replaceTextInLine |
|
|
|
|
|
|
|
} from '../components/results/SearchHelper' |
|
|
|
|
|
|
|
import { SearchReducer } from '../reducers/Reducer' |
|
|
|
|
|
|
|
import { |
|
|
|
|
|
|
|
SearchState, |
|
|
|
|
|
|
|
SearchResult, |
|
|
|
|
|
|
|
SearchResultLine, |
|
|
|
|
|
|
|
SearchResultLineLine, |
|
|
|
|
|
|
|
SearchingInitialState |
|
|
|
|
|
|
|
} from '../types' |
|
|
|
import { filePathFilter } from '@jsdevtools/file-path-filter' |
|
|
|
import { filePathFilter } from '@jsdevtools/file-path-filter' |
|
|
|
|
|
|
|
import { escapeRegExp } from 'lodash' |
|
|
|
|
|
|
|
|
|
|
|
export interface SearchingStateInterface { |
|
|
|
export interface SearchingStateInterface { |
|
|
|
state: SearchState |
|
|
|
state: SearchState |
|
|
|
setFind: (value: string) => void |
|
|
|
setFind: (value: string) => void |
|
|
|
setReplace: (value: string) => void |
|
|
|
setReplace: (value: string) => void |
|
|
|
setInclude: (value: string) => void, |
|
|
|
setInclude: (value: string) => void |
|
|
|
setExclude: (value: string) => void, |
|
|
|
setExclude: (value: string) => void |
|
|
|
setCaseSensitive: (value: boolean) => void, |
|
|
|
setCaseSensitive: (value: boolean) => void |
|
|
|
setRegex: (value: boolean) => void, |
|
|
|
setRegex: (value: boolean) => void |
|
|
|
setWholeWord: (value: boolean) => void, |
|
|
|
setWholeWord: (value: boolean) => void |
|
|
|
setSearchResults: (value: SearchResult[]) => void, |
|
|
|
setSearchResults: (value: SearchResult[]) => void |
|
|
|
findText: (path: string) => Promise<SearchResultLine[]>, |
|
|
|
findText: (path: string) => Promise<SearchResultLine[]> |
|
|
|
hightLightInPath: (result:SearchResult, line:SearchResultLineLine) => void, |
|
|
|
hightLightInPath: (result: SearchResult, line: SearchResultLineLine) => void |
|
|
|
replaceText: (result: SearchResult, line: SearchResultLineLine) => void, |
|
|
|
replaceText: (result: SearchResult, line: SearchResultLineLine) => void |
|
|
|
reloadFile: (file:string) => void, |
|
|
|
reloadFile: (file: string) => void |
|
|
|
toggleCaseSensitive: () => void, |
|
|
|
toggleCaseSensitive: () => void |
|
|
|
toggleMatchWholeWord: () => void, |
|
|
|
toggleMatchWholeWord: () => void |
|
|
|
|
|
|
|
toggleUseRegex: () => void |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export const SearchContext = createContext<SearchingStateInterface>(null) |
|
|
|
export const SearchContext = createContext<SearchingStateInterface>(null) |
|
|
@ -86,12 +96,18 @@ export const SearchProvider = ({ |
|
|
|
payload: value |
|
|
|
payload: value |
|
|
|
}) |
|
|
|
}) |
|
|
|
}, |
|
|
|
}, |
|
|
|
reloadFile: async (file:string) => { |
|
|
|
reloadFile: async (file: string) => { |
|
|
|
dispatch({ |
|
|
|
dispatch({ |
|
|
|
type: 'RELOAD_FILE', |
|
|
|
type: 'RELOAD_FILE', |
|
|
|
payload: file |
|
|
|
payload: file |
|
|
|
}) |
|
|
|
}) |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
toggleUseRegex: () => { |
|
|
|
|
|
|
|
dispatch({ |
|
|
|
|
|
|
|
type: 'TOGGLE_USE_REGEX', |
|
|
|
|
|
|
|
payload: undefined |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
}, |
|
|
|
toggleCaseSensitive: () => { |
|
|
|
toggleCaseSensitive: () => { |
|
|
|
dispatch({ |
|
|
|
dispatch({ |
|
|
|
type: 'TOGGLE_CASE_SENSITIVE', |
|
|
|
type: 'TOGGLE_CASE_SENSITIVE', |
|
|
@ -104,29 +120,47 @@ export const SearchProvider = ({ |
|
|
|
payload: undefined |
|
|
|
payload: undefined |
|
|
|
}) |
|
|
|
}) |
|
|
|
}, |
|
|
|
}, |
|
|
|
findText : async (path: string) => { |
|
|
|
findText: async (path: string) => { |
|
|
|
if(!plugin) return |
|
|
|
if (!plugin) return |
|
|
|
try { |
|
|
|
try { |
|
|
|
if(state.find.length < 3) return |
|
|
|
if (state.find.length < 3) return |
|
|
|
const text = await plugin.call('fileManager', 'readFile', path) |
|
|
|
const text = await plugin.call('fileManager', 'readFile', path) |
|
|
|
let flags = 'g' |
|
|
|
let flags = 'g' |
|
|
|
let find = state.find |
|
|
|
let find = state.find |
|
|
|
if(!state.casesensitive) flags += 'i' |
|
|
|
if (!state.casesensitive) flags += 'i' |
|
|
|
if(state.matchWord) find = `\\b${find}\\b` |
|
|
|
if (state.matchWord) find = `\\b${find}\\b` |
|
|
|
|
|
|
|
if (!state.useRegExp) find = escapeRegExp(find) |
|
|
|
const re = new RegExp(find, flags) |
|
|
|
const re = new RegExp(find, flags) |
|
|
|
const result: SearchResultLine[] = findLinesInStringWithMatch(text, re) |
|
|
|
const result: SearchResultLine[] = findLinesInStringWithMatch(text, re) |
|
|
|
return result |
|
|
|
return result |
|
|
|
} catch (e) {} |
|
|
|
} catch (e) {} |
|
|
|
}, |
|
|
|
}, |
|
|
|
hightLightInPath: async(result: SearchResult, line: SearchResultLineLine) => { |
|
|
|
hightLightInPath: async ( |
|
|
|
|
|
|
|
result: SearchResult, |
|
|
|
|
|
|
|
line: SearchResultLineLine |
|
|
|
|
|
|
|
) => { |
|
|
|
await plugin.call('editor', 'discardHighlight') |
|
|
|
await plugin.call('editor', 'discardHighlight') |
|
|
|
await plugin.call('editor', 'highlight', line.position, result.path) |
|
|
|
await plugin.call('editor', 'highlight', line.position, result.path) |
|
|
|
}, |
|
|
|
}, |
|
|
|
replaceText: async(result: SearchResult, line: SearchResultLineLine) => { |
|
|
|
replaceText: async (result: SearchResult, line: SearchResultLineLine) => { |
|
|
|
await plugin.call('editor', 'discardHighlight') |
|
|
|
try { |
|
|
|
await plugin.call('editor', 'highlight', line.position, result.path) |
|
|
|
await plugin.call('editor', 'discardHighlight') |
|
|
|
const content = await plugin.call('fileManager', 'readFile', result.path) |
|
|
|
await plugin.call('editor', 'highlight', line.position, result.path) |
|
|
|
await plugin.call('fileManager','setFile', result.path, replaceTextInLine(content, line, state.replace)) |
|
|
|
const content = await plugin.call( |
|
|
|
|
|
|
|
'fileManager', |
|
|
|
|
|
|
|
'readFile', |
|
|
|
|
|
|
|
result.path |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await plugin.call( |
|
|
|
|
|
|
|
'fileManager', |
|
|
|
|
|
|
|
'setFile', |
|
|
|
|
|
|
|
result.path, |
|
|
|
|
|
|
|
replaceTextInLine(content, line, state.replace) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
throw new Error(e) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -173,8 +207,6 @@ export const SearchProvider = ({ |
|
|
|
} |
|
|
|
} |
|
|
|
}, [state.timeStamp]) |
|
|
|
}, [state.timeStamp]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
return ( |
|
|
|
<> |
|
|
|
<> |
|
|
|
<SearchContext.Provider value={value}>{children}</SearchContext.Provider> |
|
|
|
<SearchContext.Provider value={value}>{children}</SearchContext.Provider> |
|
|
|