element-web/src/autocomplete/CommandProvider.tsx

107 lines
3.9 KiB
TypeScript
Raw Normal View History

2017-06-01 14:18:06 +00:00
/*
2017-06-01 16:29:40 +00:00
Copyright 2016 Aviral Dasgupta
2017-06-01 14:18:06 +00:00
Copyright 2017 Vector Creations Ltd
2017-11-02 18:01:28 +00:00
Copyright 2017 New Vector Ltd
Copyright 2018 Michael Telatynski <7t3chguy@gmail.com>
2017-06-01 14:18:06 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React from 'react';
2021-06-29 12:11:58 +00:00
import { _t } from '../languageHandler';
2016-06-01 11:24:21 +00:00
import AutocompleteProvider from './AutocompleteProvider';
import QueryMatcher from './QueryMatcher';
2021-06-29 12:11:58 +00:00
import { TextualCompletion } from './Components';
import { ICompletion, ISelectionRange } from "./Autocompleter";
import { Command, Commands, CommandMap } from '../SlashCommands';
2016-06-01 11:24:21 +00:00
const COMMAND_RE = /(^\/\w*)(?: .*)?/g;
2016-06-01 11:24:21 +00:00
export default class CommandProvider extends AutocompleteProvider {
matcher: QueryMatcher<Command>;
2016-06-01 11:24:21 +00:00
constructor() {
super(COMMAND_RE);
this.matcher = new QueryMatcher(Commands, {
keys: ['command', 'args', 'description'],
2021-06-29 12:11:58 +00:00
funcs: [({ aliases }) => aliases.join(" ")], // aliases
2016-06-01 11:24:21 +00:00
});
}
async getCompletions(
query: string,
selection: ISelectionRange,
force?: boolean,
limit = -1,
): Promise<ICompletion[]> {
2021-06-29 12:11:58 +00:00
const { command, range } = this.getCurrentCommand(query, selection);
if (!command) return [];
let matches = [];
2018-07-09 16:50:07 +00:00
// check if the full match differs from the first word (i.e. returns false if the command has args)
if (command[0] !== command[1]) {
// The input looks like a command with arguments, perform exact match
const name = command[1].substr(1); // strip leading `/`
if (CommandMap.has(name) && CommandMap.get(name).isEnabled()) {
// some commands, namely `me` and `ddg` don't suit having the usage shown whilst typing their arguments
if (CommandMap.get(name).hideCompletionAfterSpace) return [];
matches = [CommandMap.get(name)];
}
} else {
if (query === '/') {
// If they have just entered `/` show everything
// We exclude the limit on purpose to have a comprehensive list
matches = Commands;
2018-05-20 22:43:42 +00:00
} else {
// otherwise fuzzy match against all of the fields
matches = this.matcher.match(command[1], limit);
2018-05-13 02:18:41 +00:00
}
2016-06-01 11:24:21 +00:00
}
return matches.filter(cmd => cmd.isEnabled()).map((result) => {
let completion = result.getCommand() + ' ';
const usedAlias = result.aliases.find(alias => `/${alias}` === command[1]);
// If the command (or an alias) is the same as the one they entered, we don't want to discard their arguments
if (usedAlias || result.getCommand() === command[1]) {
completion = command[0];
}
return {
completion,
type: "command",
component: <TextualCompletion
title={`/${usedAlias || result.command}`}
subtitle={result.args}
description={_t(result.description)} />,
range,
};
});
2016-06-01 11:24:21 +00:00
}
getName() {
return '*️⃣ ' + _t('Commands');
}
renderCompletions(completions: React.ReactNode[]): React.ReactNode {
return (
<div
className="mx_Autocomplete_Completion_container_block"
role="presentation"
aria-label={_t("Command Autocomplete")}
>
{ completions }
</div>
);
}
2016-06-01 11:24:21 +00:00
}