Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Compass 2934 don not autocomplete in comments #1

Merged
merged 8 commits into from
Jun 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ecmaFeatures": {
"blockBindings": true,
"blockBindings": true
},
"globals": {
"expect": true,
Expand Down
53 changes: 52 additions & 1 deletion lib/stage-autocompleter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,43 @@ const DOLLAR = '$';
*/
const BASE_COMPLETIONS = EXPRESSION_OPERATORS.concat(CONVERSION_OPERATORS.concat(BSON_TYPES));


/**
* Multi-line comment regex
*/
const MULTI_LINE_COMMENT_RE = /\/(\*)[^*]*\*+(?:[^*\/][^*]*\*+)*\//.source;

/**
* Single-line comment regex
*/
const SINGLE_LINE_COMMENT_RE = /\/(\/)[^\n]*$/.source;

/**
* String regex
*/
const STRING_RE = /"(?:[^"\\]*|\\[\S\s])*"|'(?:[^'\\]*|\\[\S\s])*'/.source;

/**
* Division operator regex
*/
const DIVISION_OPERATOR_RE = /(?:[$\w\)\]]|\+\+|--)\s*\/(?![*\/])/.source;

/**
* Regular expression regex
*/
const REGEX_RE = /\/(?=[^*\/])[^[/\\]*(?:(?:\[(?:\\.|[^\]\\]*)*\]|\\.)[^[/\\]*)*?\/[gim]*/.source;

/**
* Find slashes regex
*/
const FIND_COMMENTS_RE = new RegExp([
MULTI_LINE_COMMENT_RE,
SINGLE_LINE_COMMENT_RE,
STRING_RE,
DIVISION_OPERATOR_RE,
REGEX_RE
].join('|'), 'gm');

/**
* Adds autocomplete suggestions based on the aggregation pipeline
* operators.
Expand Down Expand Up @@ -124,6 +161,20 @@ class StageAutoCompleter {
getCompletions(editor, session, position, prefix, done) {
// Empty prefixes do not return results.
if (prefix === '') return done(null, []);

// Comments block do not return results.
const activePosition = session.getDocument().positionToIndex(position, 0);
let isComment = false;
session.getValue().replace(FIND_COMMENTS_RE, (match, mlc, slc, start) => {
if ((mlc || slc) && start < activePosition && activePosition <= (start + match.length)) {
isComment = true;
}
return match;
});
if (isComment === true) {
return done(null, []);
}

// If the current token is a string with single or double quotes, then
// we want to use the local text completer instead of suggesting operators.
// This is so we can suggest user variable names inside the pipeline that they
Expand All @@ -141,7 +192,7 @@ class StageAutoCompleter {
this.queryAutoCompleter.getCompletions(editor, session, position, prefix, done);
} else {
const expressions = BASE_COMPLETIONS.concat(this.accumulators()).concat(this.fields);
done(null, filter(this.version, expressions, prefix));
return done(null, filter(this.version, expressions, prefix));
}
}
}
Expand Down
Loading