2016-08-17 14:57:19 +03:00
|
|
|
import React from 'react';
|
2016-09-13 13:11:52 +03:00
|
|
|
import type {Completion, SelectionRange} from './Autocompleter';
|
2016-06-21 13:16:20 +03:00
|
|
|
|
2016-06-01 14:24:21 +03:00
|
|
|
export default class AutocompleteProvider {
|
2016-06-21 13:16:20 +03:00
|
|
|
constructor(commandRegex?: RegExp, fuseOpts?: any) {
|
2016-09-13 13:11:52 +03:00
|
|
|
if (commandRegex) {
|
|
|
|
if (!commandRegex.global) {
|
2016-06-21 13:16:20 +03:00
|
|
|
throw new Error('commandRegex must have global flag set');
|
|
|
|
}
|
|
|
|
this.commandRegex = commandRegex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Of the matched commands in the query, returns the first that contains or is contained by the selection, or null.
|
|
|
|
*/
|
2016-09-13 13:11:52 +03:00
|
|
|
getCurrentCommand(query: string, selection: {start: number, end: number}, force: boolean = false): ?string {
|
|
|
|
let commandRegex = this.commandRegex;
|
|
|
|
|
|
|
|
if (force && this.shouldForceComplete()) {
|
|
|
|
commandRegex = /[^\W]+/g;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (commandRegex == null) {
|
2016-06-21 13:16:20 +03:00
|
|
|
return null;
|
2016-07-03 19:45:13 +03:00
|
|
|
}
|
2016-06-21 13:16:20 +03:00
|
|
|
|
2016-09-13 13:11:52 +03:00
|
|
|
commandRegex.lastIndex = 0;
|
2016-07-04 19:14:35 +03:00
|
|
|
|
2016-07-03 19:45:13 +03:00
|
|
|
let match;
|
2016-09-13 13:11:52 +03:00
|
|
|
while ((match = commandRegex.exec(query)) != null) {
|
2016-06-21 13:16:20 +03:00
|
|
|
let matchStart = match.index,
|
|
|
|
matchEnd = matchStart + match[0].length;
|
2016-07-03 19:45:13 +03:00
|
|
|
if (selection.start <= matchEnd && selection.end >= matchStart) {
|
|
|
|
return {
|
|
|
|
command: match,
|
|
|
|
range: {
|
|
|
|
start: matchStart,
|
|
|
|
end: matchEnd,
|
|
|
|
},
|
|
|
|
};
|
2016-06-21 13:16:20 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-03 19:45:13 +03:00
|
|
|
return {
|
|
|
|
command: null,
|
|
|
|
range: {
|
|
|
|
start: -1,
|
|
|
|
end: -1,
|
|
|
|
},
|
|
|
|
};
|
2016-06-21 13:16:20 +03:00
|
|
|
}
|
|
|
|
|
2016-09-13 13:11:52 +03:00
|
|
|
async getCompletions(query: string, selection: SelectionRange, force: boolean = false): Array<Completion> {
|
|
|
|
return [];
|
2016-06-21 13:16:20 +03:00
|
|
|
}
|
|
|
|
|
2016-06-12 14:32:46 +03:00
|
|
|
getName(): string {
|
|
|
|
return 'Default Provider';
|
|
|
|
}
|
2016-08-17 14:57:19 +03:00
|
|
|
|
|
|
|
renderCompletions(completions: [React.Component]): ?React.Component {
|
2016-08-22 22:06:31 +03:00
|
|
|
console.error('stub; should be implemented in subclasses');
|
|
|
|
return null;
|
2016-08-17 14:57:19 +03:00
|
|
|
}
|
2016-09-13 13:11:52 +03:00
|
|
|
|
|
|
|
// Whether we should provide completions even if triggered forcefully, without a sigil.
|
|
|
|
shouldForceComplete(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-01 14:24:21 +03:00
|
|
|
}
|