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
|
|
|
|
|
|
|
|
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-08-17 11:57:19 +00:00
|
|
|
import React from 'react';
|
2016-09-13 10:11:52 +00:00
|
|
|
import type {Completion, SelectionRange} from './Autocompleter';
|
2016-06-21 10:16:20 +00:00
|
|
|
|
2016-06-01 11:24:21 +00:00
|
|
|
export default class AutocompleteProvider {
|
2016-12-01 06:36:57 +00:00
|
|
|
constructor(commandRegex?: RegExp) {
|
2016-09-13 10:11:52 +00:00
|
|
|
if (commandRegex) {
|
|
|
|
if (!commandRegex.global) {
|
2016-06-21 10:16:20 +00: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 10:11:52 +00:00
|
|
|
getCurrentCommand(query: string, selection: {start: number, end: number}, force: boolean = false): ?string {
|
|
|
|
let commandRegex = this.commandRegex;
|
|
|
|
|
|
|
|
if (force && this.shouldForceComplete()) {
|
2016-09-16 10:08:29 +00:00
|
|
|
commandRegex = /\S+/g;
|
2016-09-13 10:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (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-09-13 10:11:52 +00:00
|
|
|
commandRegex.lastIndex = 0;
|
2017-01-20 14:22:27 +00:00
|
|
|
|
2016-07-03 16:45:13 +00:00
|
|
|
let match;
|
2016-09-13 10:11:52 +00:00
|
|
|
while ((match = 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-09-13 10:11:52 +00:00
|
|
|
async getCompletions(query: string, selection: SelectionRange, force: boolean = false): Array<Completion> {
|
|
|
|
return [];
|
2016-06-21 10:16:20 +00:00
|
|
|
}
|
|
|
|
|
2016-06-12 11:32:46 +00:00
|
|
|
getName(): string {
|
|
|
|
return 'Default Provider';
|
|
|
|
}
|
2016-08-17 11:57:19 +00:00
|
|
|
|
|
|
|
renderCompletions(completions: [React.Component]): ?React.Component {
|
2016-08-22 19:06:31 +00:00
|
|
|
console.error('stub; should be implemented in subclasses');
|
|
|
|
return null;
|
2016-08-17 11:57:19 +00:00
|
|
|
}
|
2016-09-13 10:11:52 +00:00
|
|
|
|
|
|
|
// Whether we should provide completions even if triggered forcefully, without a sigil.
|
|
|
|
shouldForceComplete(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-01 11:24:21 +00:00
|
|
|
}
|