2017-06-01 16:18:06 +02:00
|
|
|
/*
|
2017-06-01 18:29:40 +02:00
|
|
|
Copyright 2016 Aviral Dasgupta
|
2017-06-01 16:18:06 +02:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-06-19 13:06:13 +02:00
|
|
|
Copyright 2017, 2018 New Vector Ltd
|
2017-06-01 16:18:06 +02: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 11:01:05 +02:00
|
|
|
import React from 'react';
|
2020-04-20 20:00:54 +02:00
|
|
|
import type {ICompletion, ISelectionRange} from './Autocompleter';
|
|
|
|
|
|
|
|
export interface ICommand {
|
|
|
|
command: string | null;
|
|
|
|
range: {
|
|
|
|
start: number;
|
|
|
|
end: number;
|
|
|
|
};
|
|
|
|
}
|
2016-06-21 12:16:20 +02:00
|
|
|
|
2016-06-01 13:24:21 +02:00
|
|
|
export default class AutocompleteProvider {
|
2020-04-20 20:00:54 +02:00
|
|
|
commandRegex: RegExp;
|
|
|
|
forcedCommandRegex: RegExp;
|
|
|
|
|
2018-05-13 04:04:40 +02:00
|
|
|
constructor(commandRegex?: RegExp, forcedCommandRegex?: RegExp) {
|
2016-09-13 12:11:52 +02:00
|
|
|
if (commandRegex) {
|
|
|
|
if (!commandRegex.global) {
|
2016-06-21 12:16:20 +02:00
|
|
|
throw new Error('commandRegex must have global flag set');
|
|
|
|
}
|
|
|
|
this.commandRegex = commandRegex;
|
|
|
|
}
|
2018-05-13 04:04:40 +02:00
|
|
|
if (forcedCommandRegex) {
|
|
|
|
if (!forcedCommandRegex.global) {
|
|
|
|
throw new Error('forcedCommandRegex must have global flag set');
|
|
|
|
}
|
|
|
|
this.forcedCommandRegex = forcedCommandRegex;
|
|
|
|
}
|
2016-06-21 12:16:20 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 18:51:08 +01:00
|
|
|
destroy() {
|
2017-11-02 19:10:13 +01:00
|
|
|
// stub
|
2017-11-02 18:51:08 +01:00
|
|
|
}
|
|
|
|
|
2016-06-21 12:16:20 +02:00
|
|
|
/**
|
|
|
|
* Of the matched commands in the query, returns the first that contains or is contained by the selection, or null.
|
2019-01-11 14:54:11 +01:00
|
|
|
* @param {string} query The query string
|
2020-04-20 20:00:54 +02:00
|
|
|
* @param {ISelectionRange} selection Selection to search
|
2019-01-11 14:54:11 +01:00
|
|
|
* @param {boolean} force True if the user is forcing completion
|
2019-01-11 15:09:29 +01:00
|
|
|
* @return {object} { command, range } where both objects fields are null if no match
|
2016-06-21 12:16:20 +02:00
|
|
|
*/
|
2020-04-20 20:00:54 +02:00
|
|
|
getCurrentCommand(query: string, selection: ISelectionRange, force = false) {
|
2016-09-13 12:11:52 +02:00
|
|
|
let commandRegex = this.commandRegex;
|
|
|
|
|
|
|
|
if (force && this.shouldForceComplete()) {
|
2018-05-13 04:04:40 +02:00
|
|
|
commandRegex = this.forcedCommandRegex || /\S+/g;
|
2016-09-13 12:11:52 +02:00
|
|
|
}
|
|
|
|
|
2020-04-22 14:18:54 +02:00
|
|
|
if (!commandRegex) {
|
2016-06-21 12:16:20 +02:00
|
|
|
return null;
|
2016-07-03 18:45:13 +02:00
|
|
|
}
|
2016-06-21 12:16:20 +02:00
|
|
|
|
2016-09-13 12:11:52 +02:00
|
|
|
commandRegex.lastIndex = 0;
|
2017-01-20 15:22:27 +01:00
|
|
|
|
2016-07-03 18:45:13 +02:00
|
|
|
let match;
|
2020-04-20 20:04:55 +02:00
|
|
|
while ((match = commandRegex.exec(query)) !== null) {
|
2018-06-19 13:06:13 +02:00
|
|
|
const start = match.index;
|
|
|
|
const end = start + match[0].length;
|
|
|
|
if (selection.start <= end && selection.end >= start) {
|
2016-07-03 18:45:13 +02:00
|
|
|
return {
|
|
|
|
command: match,
|
|
|
|
range: {
|
2018-06-19 13:06:13 +02:00
|
|
|
start,
|
|
|
|
end,
|
2016-07-03 18:45:13 +02:00
|
|
|
},
|
|
|
|
};
|
2016-06-21 12:16:20 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-03 18:45:13 +02:00
|
|
|
return {
|
|
|
|
command: null,
|
|
|
|
range: {
|
|
|
|
start: -1,
|
|
|
|
end: -1,
|
|
|
|
},
|
|
|
|
};
|
2016-06-21 12:16:20 +02:00
|
|
|
}
|
|
|
|
|
2020-04-20 20:00:54 +02:00
|
|
|
async getCompletions(query: string, selection: ISelectionRange, force = false): Promise<ICompletion[]> {
|
2016-09-13 12:11:52 +02:00
|
|
|
return [];
|
2016-06-21 12:16:20 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 13:32:46 +02:00
|
|
|
getName(): string {
|
|
|
|
return 'Default Provider';
|
|
|
|
}
|
2016-08-17 13:57:19 +02:00
|
|
|
|
2020-04-20 20:00:54 +02:00
|
|
|
renderCompletions(completions: React.ReactNode[]): React.ReactNode | null {
|
2016-08-22 21:06:31 +02:00
|
|
|
console.error('stub; should be implemented in subclasses');
|
|
|
|
return null;
|
2016-08-17 13:57:19 +02:00
|
|
|
}
|
2016-09-13 12:11:52 +02:00
|
|
|
|
|
|
|
// Whether we should provide completions even if triggered forcefully, without a sigil.
|
|
|
|
shouldForceComplete(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-01 13:24:21 +02:00
|
|
|
}
|