2016-06-21 10:16:20 +00:00
|
|
|
import Q from 'q';
|
|
|
|
|
2016-06-01 11:24:21 +00:00
|
|
|
export default class AutocompleteProvider {
|
2016-06-21 10:16:20 +00:00
|
|
|
constructor(commandRegex?: RegExp, fuseOpts?: any) {
|
|
|
|
if(commandRegex) {
|
|
|
|
if(!commandRegex.global) {
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
getCurrentCommand(query: string, selection: {start: number, end: number}): ?Array<string> {
|
2016-07-03 16:45:13 +00:00
|
|
|
if (this.commandRegex == null) {
|
2016-06-21 10:16:20 +00:00
|
|
|
return null;
|
2016-07-03 16:45:13 +00:00
|
|
|
}
|
2016-06-21 10:16:20 +00:00
|
|
|
|
2016-07-04 16:14:35 +00:00
|
|
|
this.commandRegex.lastIndex = 0;
|
|
|
|
|
2016-07-03 16:45:13 +00:00
|
|
|
let match;
|
|
|
|
while ((match = this.commandRegex.exec(query)) != null) {
|
2016-06-21 10:16:20 +00:00
|
|
|
let matchStart = match.index,
|
|
|
|
matchEnd = matchStart + match[0].length;
|
|
|
|
|
2016-07-03 16:45:13 +00:00
|
|
|
if (selection.start <= matchEnd && selection.end >= matchStart) {
|
|
|
|
return {
|
|
|
|
command: match,
|
|
|
|
range: {
|
|
|
|
start: matchStart,
|
|
|
|
end: matchEnd,
|
|
|
|
},
|
|
|
|
};
|
2016-06-21 10:16:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-03 16:45:13 +00:00
|
|
|
return {
|
|
|
|
command: null,
|
|
|
|
range: {
|
|
|
|
start: -1,
|
|
|
|
end: -1,
|
|
|
|
},
|
|
|
|
};
|
2016-06-21 10:16:20 +00:00
|
|
|
}
|
|
|
|
|
2016-07-03 16:45:13 +00:00
|
|
|
getCompletions(query: string, selection: {start: number, end: number}) {
|
2016-06-21 10:16:20 +00:00
|
|
|
return Q.when([]);
|
|
|
|
}
|
|
|
|
|
2016-06-12 11:32:46 +00:00
|
|
|
getName(): string {
|
|
|
|
return 'Default Provider';
|
|
|
|
}
|
2016-06-01 11:24:21 +00:00
|
|
|
}
|