Merge pull request #1981 from matrix-org/t3chguy/improve_command_provider

improve command provider
pull/21833/head
David Baker 2018-06-18 11:51:06 +01:00 committed by GitHub
commit 1ab902d738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 18 deletions

View File

@ -2,6 +2,7 @@
Copyright 2016 Aviral Dasgupta Copyright 2016 Aviral Dasgupta
Copyright 2017 Vector Creations Ltd Copyright 2017 Vector Creations Ltd
Copyright 2017 New Vector Ltd Copyright 2017 New Vector Ltd
Copyright 2018 Michael Telatynski <7t3chguy@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -21,6 +22,7 @@ import { _t, _td } from '../languageHandler';
import AutocompleteProvider from './AutocompleteProvider'; import AutocompleteProvider from './AutocompleteProvider';
import FuzzyMatcher from './FuzzyMatcher'; import FuzzyMatcher from './FuzzyMatcher';
import {TextualCompletion} from './Components'; import {TextualCompletion} from './Components';
import type {SelectionRange} from "./Autocompleter";
// TODO merge this with the factory mechanics of SlashCommands? // TODO merge this with the factory mechanics of SlashCommands?
// Warning: Since the description string will be translated in _t(result.description), all these strings below must be in i18n/strings/en_EN.json file // Warning: Since the description string will be translated in _t(result.description), all these strings below must be in i18n/strings/en_EN.json file
@ -110,10 +112,9 @@ const COMMANDS = [
args: '', args: '',
description: _td('Opens the Developer Tools dialog'), description: _td('Opens the Developer Tools dialog'),
}, },
// Omitting `/markdown` as it only seems to apply to OldComposer
]; ];
const COMMAND_RE = /(^\/\w*)/g; const COMMAND_RE = /(^\/\w*)(?: .*)?/g;
export default class CommandProvider extends AutocompleteProvider { export default class CommandProvider extends AutocompleteProvider {
constructor() { constructor() {
@ -123,13 +124,16 @@ export default class CommandProvider extends AutocompleteProvider {
}); });
} }
async getCompletions(query: string, selection: {start: number, end: number}) { async getCompletions(query: string, selection: SelectionRange, force?: boolean) {
let completions = [];
const {command, range} = this.getCurrentCommand(query, selection); const {command, range} = this.getCurrentCommand(query, selection);
if (command) { if (!command) return [];
completions = this.matcher.match(command[0]).map((result) => {
// if the query is just `/` (and the user hit TAB or waits), show them all COMMANDS otherwise FuzzyMatch them
const matches = query === '/' ? COMMANDS : this.matcher.match(command[1]);
return matches.map((result) => {
return { return {
completion: result.command + ' ', // If the command is the same as the one they entered, we don't want to discard their arguments
completion: result.command === command[1] ? command[0] : (result.command + ' '),
component: (<TextualCompletion component: (<TextualCompletion
title={result.command} title={result.command}
subtitle={result.args} subtitle={result.args}
@ -139,8 +143,6 @@ export default class CommandProvider extends AutocompleteProvider {
}; };
}); });
} }
return completions;
}
getName() { getName() {
return '*️⃣ ' + _t('Commands'); return '*️⃣ ' + _t('Commands');