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.
|
|
|
|
*/
|
|
|
|
|
2016-07-03 16:45:13 +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';
|
2016-12-01 06:36:57 +00:00
|
|
|
import FuzzyMatcher from './FuzzyMatcher';
|
2016-07-03 16:45:13 +00:00
|
|
|
import {TextualCompletion} from './Components';
|
2018-06-19 11:06:13 +00:00
|
|
|
import type {Completion, SelectionRange} from "./Autocompleter";
|
2018-06-18 18:31:40 +00:00
|
|
|
import {CommandMap} from '../SlashCommands';
|
2016-06-01 11:24:21 +00:00
|
|
|
|
2018-06-18 18:31:40 +00:00
|
|
|
const COMMANDS = Object.values(CommandMap);
|
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 {
|
|
|
|
constructor() {
|
2016-06-21 10:16:20 +00:00
|
|
|
super(COMMAND_RE);
|
2016-12-01 06:36:57 +00:00
|
|
|
this.matcher = new FuzzyMatcher(COMMANDS, {
|
2016-07-03 16:45:13 +00:00
|
|
|
keys: ['command', 'args', 'description'],
|
2016-06-01 11:24:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-19 11:06:13 +00:00
|
|
|
async getCompletions(query: string, selection: SelectionRange, force?: boolean): Array<Completion> {
|
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)
|
|
|
|
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 `/`
|
|
|
|
if (CommandMap[name]) {
|
|
|
|
matches = [CommandMap[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
|
|
|
|
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
|
|
|
|
2018-06-18 18:32:12 +00:00
|
|
|
return matches.map((result) => ({
|
|
|
|
// 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
|
|
|
|
title={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
|
|
|
|
2016-08-17 11:57:19 +00:00
|
|
|
renderCompletions(completions: [React.Component]): ?React.Component {
|
2016-08-22 19:06:31 +00:00
|
|
|
return <div className="mx_Autocomplete_Completion_container_block">
|
2017-09-28 10:21:06 +00:00
|
|
|
{ completions }
|
2016-08-22 19:06:31 +00:00
|
|
|
</div>;
|
2016-08-17 11:57:19 +00:00
|
|
|
}
|
2016-06-01 11:24:21 +00:00
|
|
|
}
|