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 4 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
49 changes: 48 additions & 1 deletion lib/stage-autocompleter.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,53 @@ 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 getMatches = (str, regex) => {
regex.lastIndex = 0;

const matches = [];
let match = regex.exec(str);

while (match) {
matches.push(match);

if (regex.lastIndex === match.index) {
regex.lastIndex++;
}

match = regex.exec(str);
}

return matches;
};
let value = session.getValue();
const findStringsWithSlashesRe = /("(?:[^"\\]*|\\[\S\s])*"|'(?:[^'\\]*|\\[\S\s])*')/gm;
const stringSlashes = getMatches(value, findStringsWithSlashesRe);

stringSlashes.forEach((item) => {
value = [
value.slice(0, item.index),
'#'.repeat(item[0].length),
value.slice(item.index + item[0].length)
].join('');
});

Copy link
Contributor Author

@alenakhineika alenakhineika Jun 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have input like this:

{x: 'uuu//UU'} // $

we need to check if ´//´ is a real start tag of the comment, or it can be a part of a string. I couldn't create a single regex to handle it, therefore i replace stings like this which contain // with {x: #########} // $ and after it i can find real comments.

const findCommentsRe = /(?:\/\*(?:[\s\S]*?)\*\/)|(?:\s*\/\/(?:.*)$)/gm;
const comments = getMatches(value, findCommentsRe);
const activePosition = session.getDocument().positionToIndex(position, 0);
let isComment = false;

comments.forEach((item) => {
if (item.index < activePosition && activePosition <= (item.index + item[0].length)) {
isComment = true;
}
});

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 +188,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