Skip to content
This repository was archived by the owner on Apr 1, 2020. It is now read-only.

Allow for completions filter to be case insensitive #2249

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions browser/src/Services/Completion/CompletionSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Selectors are functions that take a state and derive a value from it.
*/

import { configuration } from "./../Configuration"
import { ICompletionState } from "./CompletionState"

import * as types from "vscode-languageserver-types"
Expand All @@ -12,6 +13,31 @@ const EmptyCompletions: types.CompletionItem[] = []

import * as CompletionUtility from "./CompletionUtility"

export const shouldFilterBeCaseSensitive = (searchString: string): boolean => {
// TODO: Technically, this makes the reducer 'impure',
// which is not ideal - need to refactor eventually.
//
// One option is to plumb through the configuration setting
// from the top-level, but it might be worth extracting
// out the filter strategy in general.
const caseSensitivitySetting = configuration.getValue("editor.completions.caseSensitive")

if (caseSensitivitySetting === false) {
return false
} else if (caseSensitivitySetting === true) {
return true
} else {
// "Smart" casing strategy
// If the string is all lower-case, not case sensitive..
if (searchString === searchString.toLowerCase()) {
return false
// Otherwise, it is case sensitive..
} else {
return true
}
}
}

export const getFilteredCompletions = (state: ICompletionState): types.CompletionItem[] => {
if (!state.completionResults.completions || !state.completionResults.completions.length) {
return EmptyCompletions
Expand Down Expand Up @@ -72,12 +98,13 @@ export const filterCompletionOptions = (
return null
}

const filterRegEx = new RegExp("^" + searchText.split("").join(".*") + ".*")
const isCaseSensitive = shouldFilterBeCaseSensitive(searchText)

const filteredOptions = items.filter(f => {
const textToFilterOn = f.filterText || f.label
return textToFilterOn.match(filterRegEx)
})
if (!isCaseSensitive) {
searchText = searchText.toLocaleLowerCase()
}

const filteredOptions = processSearchText(searchText, items, isCaseSensitive)

return filteredOptions.sort((itemA, itemB) => {
const itemAFilterText = itemA.filterText || itemA.label
Expand All @@ -89,3 +116,21 @@ export const filterCompletionOptions = (
return indexOfB - indexOfA
})
}

export const processSearchText = (
searchText: string,
items: types.CompletionItem[],
isCaseSensitive: boolean,
): types.CompletionItem[] => {
const filterRegExp = new RegExp(".*" + searchText.split("").join(".*") + ".*")

return items.filter(f => {
let textToFilterOn = f.filterText || f.label

if (!isCaseSensitive) {
textToFilterOn = textToFilterOn.toLowerCase()
}

return textToFilterOn.match(filterRegExp)
})
}
5 changes: 4 additions & 1 deletion browser/src/Services/Configuration/DefaultConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const BaseConfiguration: IConfigurationValues = {
"editor.quickInfo.delay": 500,

"editor.completions.mode": "oni",
"editor.completions.caseSensitive": true,
"editor.errors.slideOnFocus": true,
"editor.formatting.formatOnSwitchToNormalMode": false,

Expand Down Expand Up @@ -459,7 +460,9 @@ const LinuxConfigOverrides: Partial<IConfigurationValues> = {

const PlatformConfigOverride = Platform.isWindows()
? WindowsConfigOverrides
: Platform.isLinux() ? LinuxConfigOverrides : MacConfigOverrides
: Platform.isLinux()
? LinuxConfigOverrides
: MacConfigOverrides

export const DefaultConfiguration = {
...BaseConfiguration,
Expand Down
3 changes: 3 additions & 0 deletions browser/src/Services/Configuration/IConfigurationValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ export interface IConfigurationValues {
// a custom init.vim, as that may cause problematic behavior
"editor.completions.mode": string

// Decide whether or not the completion matching should be case sensitive
"editor.completions.caseSensitive": boolean | string

// If true (default), ligatures are enabled
"editor.fontLigatures": boolean
"editor.fontSize": string
Expand Down