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
|
2018-06-16 15:40:44 +00:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 09:01:05 +00:00
|
|
|
import React from 'react';
|
2018-06-18 18:31:40 +00:00
|
|
|
import {_t} from '../languageHandler';
|
2016-06-01 11:24:21 +00:00
|
|
|
import AutocompleteProvider from './AutocompleteProvider';
|
2018-08-13 18:15:42 +00:00
|
|
|
import QueryMatcher from './QueryMatcher';
|
2016-07-03 16:45:13 +00:00
|
|
|
import {TextualCompletion} from './Components';
|
2020-04-20 18:00:54 +00:00
|
|
|
import {ICompletion, ISelectionRange} from "./Autocompleter";
|
|
|
|
import {Command, Commands, CommandMap} from '../SlashCommands';
|
2016-06-01 11:24:21 +00:00
|
|
|
|
2018-06-16 15:40:44 +00:00
|
|
|
const COMMAND_RE = /(^\/\w*)(?: .*)?/g;
|
2016-06-21 10:16:20 +00:00
|
|
|
|
2016-06-01 11:24:21 +00:00
|
|
|
export default class CommandProvider extends AutocompleteProvider {
|
2020-04-20 18:00:54 +00:00
|
|
|
matcher: QueryMatcher<Command>;
|
|
|
|
|
2016-06-01 11:24:21 +00:00
|
|
|
constructor() {
|
2016-06-21 10:16:20 +00:00
|
|
|
super(COMMAND_RE);
|
2020-03-30 12:59:08 +00:00
|
|
|
this.matcher = new QueryMatcher(Commands, {
|
|
|
|
keys: ['command', 'args', 'description'],
|
|
|
|
funcs: [({aliases}) => aliases.join(" ")], // aliases
|
2016-06-01 11:24:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:00:54 +00:00
|
|
|
async getCompletions(query: string, selection: ISelectionRange, force?: boolean): Promise<ICompletion[]> {
|
2017-05-29 22:52:55 +00:00
|
|
|
const {command, range} = this.getCurrentCommand(query, selection);
|
2018-06-16 15:40:44 +00:00
|
|
|
if (!command) return [];
|
|
|
|
|
2018-06-20 16:21:06 +00:00
|
|
|
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)
|
2018-07-09 19:14:37 +00:00
|
|
|
if (command[0] !== command[1]) {
|
2018-06-18 16:37:55 +00:00
|
|
|
// The input looks like a command with arguments, perform exact match
|
2018-06-20 16:25:23 +00:00
|
|
|
const name = command[1].substr(1); // strip leading `/`
|
2020-03-30 12:59:08 +00:00
|
|
|
if (CommandMap.has(name)) {
|
2018-07-18 14:38:21 +00:00
|
|
|
// some commands, namely `me` and `ddg` don't suit having the usage shown whilst typing their arguments
|
2020-03-30 12:59:08 +00:00
|
|
|
if (CommandMap.get(name).hideCompletionAfterSpace) return [];
|
|
|
|
matches = [CommandMap.get(name)];
|
2018-06-18 16:37:55 +00:00
|
|
|
}
|
2018-06-20 16:21:06 +00:00
|
|
|
} else {
|
2018-06-18 16:37:55 +00:00
|
|
|
if (query === '/') {
|
|
|
|
// If they have just entered `/` show everything
|
2020-03-30 12:59:08 +00:00
|
|
|
matches = Commands;
|
2018-05-20 22:43:42 +00:00
|
|
|
} else {
|
2018-06-18 16:37:55 +00:00
|
|
|
// otherwise fuzzy match against all of the fields
|
|
|
|
matches = this.matcher.match(command[1]);
|
2018-05-13 02:18:41 +00:00
|
|
|
}
|
2016-06-01 11:24:21 +00:00
|
|
|
}
|
2018-06-18 16:37:55 +00:00
|
|
|
|
2020-03-30 12:59:08 +00:00
|
|
|
|
|
|
|
return matches.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
|
|
|
}
|
2016-06-12 11:32:46 +00:00
|
|
|
|
|
|
|
getName() {
|
2017-05-23 14:16:31 +00:00
|
|
|
return '*️⃣ ' + _t('Commands');
|
2016-06-12 11:32:46 +00:00
|
|
|
}
|
2016-06-20 08:22:55 +00:00
|
|
|
|
2020-04-20 18:00:54 +00:00
|
|
|
renderCompletions(completions: React.ReactNode[]): React.ReactNode {
|
2019-10-14 09:37:10 +00:00
|
|
|
return (
|
2020-08-29 00:11:08 +00:00
|
|
|
<div
|
|
|
|
className="mx_Autocomplete_Completion_container_block"
|
|
|
|
role="listbox"
|
|
|
|
aria-label={_t("Command Autocomplete")}
|
|
|
|
>
|
2019-10-14 09:37:10 +00:00
|
|
|
{ completions }
|
|
|
|
</div>
|
|
|
|
);
|
2016-08-17 11:57:19 +00:00
|
|
|
}
|
2016-06-01 11:24:21 +00:00
|
|
|
}
|