getTextStyle method

TextStyle getTextStyle(
  1. String token,
  2. dynamic spans,
  3. dynamic lastToken
)

Implementation

TextStyle getTextStyle(String token, spans, lastToken){

  var keywords = [
     'abstract', 'as', 'base', 'bool', 'break', 'byte', 'case', 'catch', 'char', 'checked', 'class', 'const', 'continue',
      'decimal', 'default', 'delegate', 'do', 'double', 'else', 'enum', 'event', 'explicit', 'extern', 'false', 'finally',
      'fixed', 'float', 'for', 'foreach', 'goto', 'if', 'implicit', 'in', 'int', 'interface', 'internal', 'is', 'lock', 'long',
      'namespace', 'new', 'null', 'object', 'operator', 'out', 'override', 'params', 'private', 'protected', 'public', 'readonly',
      'ref', 'return', 'sbyte', 'sealed', 'short', 'sizeof', 'stackalloc', 'static', 'string', 'struct', 'switch', 'this', 'throw',
      'true', 'try', 'typeof', 'uint', 'ulong', 'unchecked', 'unsafe', 'ushort', 'using', 'virtual', 'void', 'volatile', 'while'
    ]
  ;
  var specialIdentifiers = [
    'add', 'alias', 'ascending', 'async', 'await', 'by', 'descending', 'dynamic', 'equals', 'from',
    'get', 'global', 'group', 'into', 'join', 'let', 'nameof', 'notnull', 'on', 'orderby', 'partial', 'partial', 'remove',
    'select', 'set', 'unmanaged', 'value', 'var', 'when', 'where', 'where', 'with', 'yield'
  ];
  var operators = ['+','-','*','/','%', '!','?' , ':' , '~' ,'^','&','|','=','<','>','>>','<<','++','--','!=','<=','>=', '=>','+=','-=','*=','==','/=','%=','|=','&=','^=','>>=','<<=','&&','||'];
  var re = new RegExp(r'\w+');
  if(re.stringMatch(token) == token){
    if (keywords.contains(token)) {
      return theme.keyword;
    } else if (specialIdentifiers.contains(token))
      return theme.specialIdentifier;
    else if((new RegExp(r'\d+')).stringMatch(token) == token)
      return theme.numberConstant;
    return theme.identifier;
  }else{
    if((new RegExp(r'(//.*)|(/\*(.|\n)*?\*/)')).stringMatch(token) == token)
      return theme.comment;
    else if((new RegExp(r'"(\\\n|\\"|[^"\n])*"' + '|' + r"'(\\\n|\\'|[^'\n])*'" + '|' + r'@"(\\"|[^"]|\n)*"')).stringMatch(token) == token)
      return theme.stringConstant;
    else if(operators.contains(token.trim()))
      return theme.operator;
    var lt = token.trimLeft();
    if(lt.length>0)
      if(lt[0]=="("  && (new RegExp(r'\w+')).stringMatch(lastToken) == lastToken && !(specialIdentifiers.contains(lastToken) || keywords.contains(lastToken))){//it is a function
        spans.removeAt(spans.length-1);
        spans.add(TextSpan(text: lastToken, style: theme.functionIdentifier));
      }
    return theme.specialCharacter;
  }
}