This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c64c957
commit 4e4df33
Showing
16 changed files
with
370 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// @flow | ||
|
||
import { find, findNext, findPrev, removeOverlay } from "../utils/editor"; | ||
import { getMatches } from "../workers/search"; | ||
import type { ThunkArgs } from "./types"; | ||
|
||
import { | ||
getSelectedSource, | ||
getFileSearchModifiers, | ||
getFileSearchQuery, | ||
getFileSearchResults | ||
} from "../selectors"; | ||
|
||
import { | ||
closeActiveSearch, | ||
clearHighlightLineRange, | ||
setActiveSearch | ||
} from "./ui"; | ||
type Editor = Object; | ||
type Match = Object; | ||
|
||
export function doSearch(query: string, editor: Editor) { | ||
return ({ getState, dispatch }: ThunkArgs) => { | ||
const selectedSource = getSelectedSource(getState()); | ||
if (!selectedSource || !selectedSource.get("text")) { | ||
return; | ||
} | ||
|
||
dispatch(setFileSearchQuery(query)); | ||
dispatch(searchContents(query, editor)); | ||
}; | ||
} | ||
|
||
export function setFileSearchQuery(query: string) { | ||
return { | ||
type: "UPDATE_FILE_SEARCH_QUERY", | ||
query | ||
}; | ||
} | ||
|
||
export function toggleFileSearchModifier(modifier: string) { | ||
return { type: "TOGGLE_FILE_SEARCH_MODIFIER", modifier }; | ||
} | ||
|
||
export function updateSearchResults( | ||
characterIndex: number, | ||
line: number, | ||
matches: Match[] | ||
) { | ||
const matchIndex = matches.findIndex( | ||
elm => elm.line === line && elm.ch === characterIndex | ||
); | ||
|
||
return { | ||
type: "UPDATE_SEARCH_RESULTS", | ||
results: { | ||
matches, | ||
matchIndex, | ||
count: matches.length, | ||
index: characterIndex | ||
} | ||
}; | ||
} | ||
|
||
export function searchContents(query: string, editor: Object) { | ||
return async ({ getState, dispatch }: ThunkArgs) => { | ||
const modifiers = getFileSearchModifiers(getState()); | ||
const selectedSource = getSelectedSource(getState()); | ||
|
||
if ( | ||
!query || | ||
!editor || | ||
!selectedSource || | ||
!selectedSource.get("text") || | ||
!modifiers | ||
) { | ||
return; | ||
} | ||
|
||
const ctx = { ed: editor, cm: editor.codeMirror }; | ||
const _modifiers = modifiers.toJS(); | ||
const matches = await getMatches( | ||
query, | ||
selectedSource.get("text"), | ||
_modifiers | ||
); | ||
const { ch, line } = find(ctx, query, true, _modifiers); | ||
|
||
dispatch(updateSearchResults(ch, line, matches)); | ||
}; | ||
} | ||
|
||
export function traverseResults(rev: boolean, editor: Editor) { | ||
return async ({ getState, dispatch }: ThunkArgs) => { | ||
if (!editor) { | ||
return; | ||
} | ||
|
||
const ctx = { ed: editor, cm: editor.codeMirror }; | ||
|
||
const query = getFileSearchQuery(getState()); | ||
const modifiers = getFileSearchModifiers(getState()); | ||
const { matches } = getFileSearchResults(getState()); | ||
|
||
if (query === "") { | ||
dispatch(setActiveSearch("file")); | ||
} | ||
|
||
if (modifiers) { | ||
const matchedLocations = matches || []; | ||
const { ch, line } = rev | ||
? findPrev(ctx, query, true, modifiers.toJS()) | ||
: findNext(ctx, query, true, modifiers.toJS()); | ||
|
||
console.log(line); | ||
dispatch(updateSearchResults(ch, line, matchedLocations)); | ||
} | ||
}; | ||
} | ||
|
||
export function closeFileSearch(editor: Editor) { | ||
return ({ getState, dispatch }: ThunkArgs) => { | ||
const modifiers = getFileSearchModifiers(getState()); | ||
const query = getFileSearchQuery(getState()); | ||
|
||
if (editor && modifiers) { | ||
const ctx = { ed: editor, cm: editor.codeMirror }; | ||
removeOverlay(ctx, query, modifiers.toJS()); | ||
} | ||
|
||
dispatch(setFileSearchQuery("")); | ||
dispatch(closeActiveSearch()); | ||
dispatch(clearHighlightLineRange()); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { createStore, selectors, actions } from "../../utils/test-head"; | ||
|
||
const { | ||
getFileSearchQuery, | ||
getFileSearchModifiers, | ||
getFileSearchResults | ||
} = selectors; | ||
|
||
describe("file text search", () => { | ||
it("should update search results", () => { | ||
const { dispatch, getState } = createStore(); | ||
expect(getFileSearchResults(getState())).toEqual({ | ||
matches: [], | ||
matchIndex: -1, | ||
index: -1, | ||
count: 0 | ||
}); | ||
|
||
const matches = [{ line: 1, ch: 3 }, { line: 3, ch: 2 }]; | ||
dispatch(actions.updateSearchResults(2, 3, matches)); | ||
|
||
expect(getFileSearchResults(getState())).toEqual({ | ||
count: 2, | ||
index: 2, | ||
matchIndex: 1, | ||
matches: matches | ||
}); | ||
}); | ||
|
||
it("should update the file search query", () => { | ||
const { dispatch, getState } = createStore(); | ||
let fileSearchQueryState = getFileSearchQuery(getState()); | ||
expect(fileSearchQueryState).toBe(""); | ||
dispatch(actions.setFileSearchQuery("foobar")); | ||
fileSearchQueryState = getFileSearchQuery(getState()); | ||
expect(fileSearchQueryState).toBe("foobar"); | ||
}); | ||
|
||
it("should toggle a file search modifier", () => { | ||
const { dispatch, getState } = createStore(); | ||
let fileSearchModState = getFileSearchModifiers(getState()); | ||
expect(fileSearchModState.get("caseSensitive")).toBe(false); | ||
dispatch(actions.toggleFileSearchModifier("caseSensitive")); | ||
fileSearchModState = getFileSearchModifiers(getState()); | ||
expect(fileSearchModState.get("caseSensitive")).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.