Skip to content

Commit

Permalink
feat(search): add multi-word search #143 (#144)
Browse files Browse the repository at this point in the history
feat(search): add multi-word search #143 (#144)
  • Loading branch information
antonreshetov authored Mar 19, 2021
1 parent 360dd2f commit cbb2041
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/renderer/components/editor/MonacoEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ export default {
},
searchHighlight (query) {
const model = this.editor.getModel()
const matches = model.findMatches(query, false, true, false)
const re = new RegExp(query ? query.replace(' ', '|') : null)
const matches = model.findMatches(re, false, true, false)
const newDecorations = matches.map(i => {
return {
range: i.range,
Expand Down
13 changes: 8 additions & 5 deletions src/renderer/store/modules/snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,19 @@ export default {
await dispatch('getSnippets')
commit('RESET_ACTIVE_FRAGMENT')

query = query.toLowerCase()
query = query.trim().toLowerCase()
const re = new RegExp(query.replace(' ', '|'))

const snippets = getters.snippetsBySort

const resultBySnippetContent = snippets.filter(snippet =>
snippet.content.some(content =>
content.value ? content.value.toLowerCase().includes(query) : false
content.value ? content.value.toLowerCase().match(re)?.length : false
)
)

const resultBySnippetName = snippets.filter(snippet =>
snippet.name.toLowerCase().includes(query)
snippet.name.toLowerCase().match(re)?.length
)

const results = uniqBy(
Expand Down Expand Up @@ -400,15 +402,16 @@ export default {

query = query.toLowerCase()
const snippets = getters.snippetsBySort
const re = new RegExp(query.replace(' ', '|'))

const resultBySnippetContent = snippets.filter(snippet =>
snippet.content.some(content =>
content.value ? content.value.toLowerCase().includes(query) : false
content.value ? content.value.toLowerCase().match(re)?.length : false
)
)

const resultBySnippetName = snippets.filter(snippet =>
snippet.name.toLowerCase().includes(query)
snippet.name.toLowerCase().match(re)?.length
)

const results = uniqBy(
Expand Down

0 comments on commit cbb2041

Please # to comment.