2017-06-01 15:18:06 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2017-2024 New Vector Ltd.
|
2017-06-01 15:18:06 +01:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2016 Aviral Dasgupta
|
2017-06-01 15:18:06 +01:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2017-06-01 15:18:06 +01:00
|
|
|
*/
|
|
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import type React from "react";
|
2021-11-18 12:47:11 +00:00
|
|
|
import { TimelineRenderingType } from "../contexts/RoomContext";
|
2021-06-29 13:11:58 +01:00
|
|
|
import type { ICompletion, ISelectionRange } from "./Autocompleter";
|
2020-04-20 19:00:54 +01:00
|
|
|
|
|
|
|
|
export interface ICommand {
|
2023-01-12 13:25:14 +00:00
|
|
|
command: RegExpExecArray | null;
|
2020-04-20 19:00:54 +01:00
|
|
|
range: {
|
|
|
|
|
start: number;
|
|
|
|
|
end: number;
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-06-21 15:46:20 +05:30
|
|
|
|
2021-11-18 12:47:11 +00:00
|
|
|
export interface IAutocompleteOptions {
|
|
|
|
|
commandRegex?: RegExp;
|
|
|
|
|
forcedCommandRegex?: RegExp;
|
|
|
|
|
renderingType?: TimelineRenderingType;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-18 10:55:24 +00:00
|
|
|
export default abstract class AutocompleteProvider {
|
2023-05-05 09:11:14 +01:00
|
|
|
public commandRegex?: RegExp;
|
|
|
|
|
public forcedCommandRegex?: RegExp;
|
2020-04-20 19:00:54 +01:00
|
|
|
|
2021-11-18 12:47:11 +00:00
|
|
|
protected renderingType: TimelineRenderingType = TimelineRenderingType.Room;
|
|
|
|
|
|
|
|
|
|
protected constructor({ commandRegex, forcedCommandRegex, renderingType }: IAutocompleteOptions) {
|
2016-09-13 15:41:52 +05:30
|
|
|
if (commandRegex) {
|
|
|
|
|
if (!commandRegex.global) {
|
2016-06-21 15:46:20 +05:30
|
|
|
throw new Error("commandRegex must have global flag set");
|
|
|
|
|
}
|
|
|
|
|
this.commandRegex = commandRegex;
|
|
|
|
|
}
|
2018-05-13 03:04:40 +01:00
|
|
|
if (forcedCommandRegex) {
|
|
|
|
|
if (!forcedCommandRegex.global) {
|
|
|
|
|
throw new Error("forcedCommandRegex must have global flag set");
|
|
|
|
|
}
|
|
|
|
|
this.forcedCommandRegex = forcedCommandRegex;
|
|
|
|
|
}
|
2021-11-18 12:47:11 +00:00
|
|
|
if (renderingType) {
|
|
|
|
|
this.renderingType = renderingType;
|
|
|
|
|
}
|
2016-06-21 15:46:20 +05:30
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public destroy(): void {
|
2017-11-02 18:10:13 +00:00
|
|
|
// stub
|
2017-11-02 17:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
2016-06-21 15:46:20 +05:30
|
|
|
/**
|
|
|
|
|
* Of the matched commands in the query, returns the first that contains or is contained by the selection, or null.
|
2019-01-11 13:54:11 +00:00
|
|
|
* @param {string} query The query string
|
2020-04-20 19:00:54 +01:00
|
|
|
* @param {ISelectionRange} selection Selection to search
|
2019-01-11 13:54:11 +00:00
|
|
|
* @param {boolean} force True if the user is forcing completion
|
2019-01-11 14:09:29 +00:00
|
|
|
* @return {object} { command, range } where both objects fields are null if no match
|
2016-06-21 15:46:20 +05:30
|
|
|
*/
|
2023-02-13 17:01:43 +00:00
|
|
|
public getCurrentCommand(query: string, selection: ISelectionRange, force = false): Partial<ICommand> {
|
2016-09-13 15:41:52 +05:30
|
|
|
let commandRegex = this.commandRegex;
|
|
|
|
|
|
|
|
|
|
if (force && this.shouldForceComplete()) {
|
2018-05-13 03:04:40 +01:00
|
|
|
commandRegex = this.forcedCommandRegex || /\S+/g;
|
2016-09-13 15:41:52 +05:30
|
|
|
}
|
|
|
|
|
|
2020-04-22 13:18:54 +01:00
|
|
|
if (!commandRegex) {
|
2023-02-13 17:01:43 +00:00
|
|
|
return {};
|
2016-07-03 22:15:13 +05:30
|
|
|
}
|
2016-06-21 15:46:20 +05:30
|
|
|
|
2016-09-13 15:41:52 +05:30
|
|
|
commandRegex.lastIndex = 0;
|
2017-01-20 14:22:27 +00:00
|
|
|
|
2023-02-03 15:27:47 +00:00
|
|
|
let match: RegExpExecArray | null;
|
2020-04-20 19:04:55 +01:00
|
|
|
while ((match = commandRegex.exec(query)) !== null) {
|
2018-06-19 12:06:13 +01:00
|
|
|
const start = match.index;
|
|
|
|
|
const end = start + match[0].length;
|
|
|
|
|
if (selection.start <= end && selection.end >= start) {
|
2016-07-03 22:15:13 +05:30
|
|
|
return {
|
|
|
|
|
command: match,
|
|
|
|
|
range: {
|
2018-06-19 12:06:13 +01:00
|
|
|
start,
|
|
|
|
|
end,
|
2016-07-03 22:15:13 +05:30
|
|
|
},
|
|
|
|
|
};
|
2016-06-21 15:46:20 +05:30
|
|
|
}
|
|
|
|
|
}
|
2016-07-03 22:15:13 +05:30
|
|
|
return {
|
|
|
|
|
command: null,
|
|
|
|
|
range: {
|
|
|
|
|
start: -1,
|
|
|
|
|
end: -1,
|
|
|
|
|
},
|
|
|
|
|
};
|
2016-06-21 15:46:20 +05:30
|
|
|
}
|
|
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public abstract getCompletions(
|
2021-05-12 12:18:56 +01:00
|
|
|
query: string,
|
|
|
|
|
selection: ISelectionRange,
|
2021-05-18 12:49:11 +01:00
|
|
|
force: boolean,
|
|
|
|
|
limit: number,
|
2021-05-18 12:56:23 +01:00
|
|
|
): Promise<ICompletion[]>;
|
2016-06-21 15:46:20 +05:30
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public abstract getName(): string;
|
2016-08-17 17:27:19 +05:30
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public abstract renderCompletions(completions: React.ReactNode[]): React.ReactNode | null;
|
2016-09-13 15:41:52 +05:30
|
|
|
|
|
|
|
|
// Whether we should provide completions even if triggered forcefully, without a sigil.
|
2022-12-16 12:29:59 +00:00
|
|
|
public shouldForceComplete(): boolean {
|
2016-09-13 15:41:52 +05:30
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-01 16:54:21 +05:30
|
|
|
}
|