Skip to content

Instantly share code, notes, and snippets.

@RSDuck
Last active October 25, 2017 02:23
Show Gist options
  • Save RSDuck/b7a4cc9fdd68504a9f6a1717eb08edba to your computer and use it in GitHub Desktop.
Save RSDuck/b7a4cc9fdd68504a9f6a1717eb08edba to your computer and use it in GitHub Desktop.
anonymous structure completion
static var reFieldPart = ~/(\.|@(:?))(\w*)$/;
static function calculateCompletionPosition(text:String, index:Int):CompletionPosition {
if (reFieldPart.match(text))
return {
pos: index - reFieldPart.matched(3).length,
toplevel: false,
};
var bracketLevel = 0;
var i = index;
var isAnonymousStruct = false;
var bracketStart = -1;
var foundComma = false;
var editingIdent = true;
while (i >= 0) {
switch (text.charAt(i)) {
case '}' | ')' | ']' | '>':
++bracketLevel;
case '{' | '(' | '[' | '<':
if(bracketLevel == 0 && text.charAt(i) == '{' && bracketStart == -1) {
bracketStart = i + 1;
++bracketLevel;
} else if(bracketLevel == 0 && text.charAt(i) == '(' && bracketStart != -1) {
isAnonymousStruct = true;
break;
}
--bracketLevel;
case ',':
if(bracketLevel == 0 && bracketStart == -1)
foundComma = true;
case ':':
if(bracketLevel == 0 && !foundComma && bracketStart == -1)
editingIdent = false;
default:
}
//if(bracketLevel < 0) break;
i--;
}
if(isAnonymousStruct && editingIdent && bracketStart != -1){
return {
pos: bracketStart,
toplevel: false,
};}
var whitespaceAmount = text.length - text.rtrim().length;
return {
pos: index - whitespaceAmount,
toplevel: true,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment