-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(search-indexes): fields autocomplete COMPASS-7174 (#4927)
- Loading branch information
Showing
11 changed files
with
159 additions
and
3 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
33 changes: 33 additions & 0 deletions
33
packages/compass-editor/src/codemirror/search-index-autocompleter.test.ts
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,33 @@ | ||
import { expect } from 'chai'; | ||
import { createSearchIndexAutocompleter } from './search-index-autocompleter'; | ||
import { setupCodemirrorCompleter } from '../../test/completer'; | ||
|
||
describe('search-index autocompleter', function () { | ||
const { getCompletions, cleanup } = setupCodemirrorCompleter( | ||
createSearchIndexAutocompleter | ||
); | ||
|
||
after(cleanup); | ||
|
||
it('returns words in context when its not completing fields', function () { | ||
const completions = getCompletions('{ dynamic: true, type: "dy', { | ||
fields: ['_id', 'name', 'age'], | ||
}); | ||
expect(completions.map((x) => x.label)).to.deep.equal([ | ||
'dynamic', | ||
'true', | ||
'type', | ||
]); | ||
}); | ||
|
||
it('returns field names when autocompleting fields', function () { | ||
const completions = getCompletions('{ fields: { "a', { | ||
fields: ['_id', 'name', 'age'], | ||
}); | ||
expect(completions.map((x) => x.label)).to.deep.equal([ | ||
'_id', | ||
'name', | ||
'age', | ||
]); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/compass-editor/src/codemirror/search-index-autocompleter.ts
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,45 @@ | ||
import type { CompletionSource } from '@codemirror/autocomplete'; | ||
import type { CompletionOptions } from '../autocompleter'; | ||
import { completer } from '../autocompleter'; | ||
import { | ||
ID_REGEX, | ||
createCompletionResultForIdPrefix, | ||
} from './ace-compat-autocompleter'; | ||
import { | ||
completeWordsInString, | ||
getAncestryOfToken, | ||
resolveTokenAtCursor, | ||
} from './utils'; | ||
|
||
const isCompletingFields = (ancestors: string[]) => { | ||
return ancestors[ancestors.length - 1] === 'fields'; | ||
}; | ||
|
||
export const createSearchIndexAutocompleter = ( | ||
options: Pick<CompletionOptions, 'fields'> = {} | ||
): CompletionSource => { | ||
const completions = completer('', { | ||
meta: ['field:identifier'], | ||
...options, | ||
}); | ||
|
||
return (context) => { | ||
const token = resolveTokenAtCursor(context); | ||
const document = context.state.sliceDoc(0); | ||
const prefix = context.matchBefore(ID_REGEX); | ||
if (!prefix) { | ||
return null; | ||
} | ||
|
||
const ancestors = getAncestryOfToken(token, document); | ||
|
||
if (isCompletingFields(ancestors)) { | ||
return createCompletionResultForIdPrefix({ | ||
prefix, | ||
completions, | ||
}); | ||
} | ||
|
||
return completeWordsInString(context); | ||
}; | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { AnyAction } from 'redux'; | ||
import { isAction } from './../utils/is-action'; | ||
|
||
export enum ActionTypes { | ||
SetFields = 'indexes/SetFields', | ||
} | ||
|
||
export type Field = { | ||
name: string; | ||
description: string; | ||
}; | ||
|
||
type SetFieldsAction = { | ||
type: ActionTypes.SetFields; | ||
fields: Field[]; | ||
}; | ||
|
||
type State = Field[]; | ||
|
||
export const INITIAL_STATE: State = []; | ||
|
||
export default function reducer(state = INITIAL_STATE, action: AnyAction) { | ||
if (isAction<SetFieldsAction>(action, ActionTypes.SetFields)) { | ||
return action.fields; | ||
} | ||
return state; | ||
} | ||
|
||
export const setFields = (fields: Field[]): SetFieldsAction => ({ | ||
type: ActionTypes.SetFields, | ||
fields, | ||
}); |
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