Last active
August 29, 2015 14:17
-
-
Save kasperpeulen/b7660a189e5cd4cc38a4 to your computer and use it in GitHub Desktop.
Parameter info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String test = "print(print(as<caret>df),sadfsd,asdf)"; | |
String test2 = "print(print(asdf),sadfsd,as<caret>df)"; | |
String test3 = "print(print(asdf),sadfsd,asdf); int a = 3<caret>;"; | |
String test4 = "if (1 + 1 <caret> == 2)"; | |
String test5 = "asdf"; | |
void main() { | |
print(parameterInfo(test,test.lastIndexOf("<caret>"))); | |
print(parameterInfo(test2,test2.lastIndexOf("<caret>"))); | |
print(parameterInfo(test3,test3.lastIndexOf("<caret>"))); | |
print(parameterInfo(test4,test4.lastIndexOf("<caret>"))); | |
print(parameterInfo(test5,test5.lastIndexOf("<caret>"))); | |
} | |
///Determines parameterInfo for a dart source. | |
///Returns null if caret is not inside parenthesis | |
///dart-services can determine | |
///if there really is a method/functionat parameterInfo["beginParen"]-1 | |
Map<String,int> parameterInfo(String source, int offset) { | |
offset += -1; | |
int parameterPosition = 0; | |
int beginParen; | |
bool skip = false; | |
while (beginParen == null && offset > 0) { | |
if (source[offset] == ",") { | |
parameterPosition += 1; | |
} | |
if (source[offset] == "(" && !skip) { | |
beginParen = offset; | |
continue; | |
} else if (source[offset] == ")") { | |
skip = true; | |
} else if (source[offset] == ";") { | |
return null; | |
} else if (skip == true && source[offset] == "(") { | |
skip = false; | |
} | |
offset += -1; | |
} | |
return beginParen == null ? null : { | |
"beginParen" : beginParen, | |
"parameterPosition" : parameterPosition | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment