public static RustTokenTypes LexerTokenToRustToken(string text, int tok) { RustTokenTypes ty = _tt[tok]; if (ty == RustTokenTypes.IDENT) { if (_kws.Contains(text)) { ty = RustTokenTypes.KEYWORD; } else if (primitiveTypes.Contains(text)) { ty = RustTokenTypes.PRIMITIVE_TYPE; } else if (wellKnownTypes.Contains(text)) { ty = RustTokenTypes.TYPE; } else { ty = RustTokenTypes.IDENT; } } return(ty); }
// Parses racer output into completions. private IEnumerable <Completion> GetCompletions(RustTokenTypes tokenType, string tokenText, string racerResponse) { // Completions from racer. var lines = racerResponse.Split(new[] { '\n' }, StringSplitOptions.None).Where(l => l.StartsWith("MATCH")); foreach (var line in lines) { var tokens = line.Substring(6).Split(','); string text = tokens[0]; string langElemText = tokens[4]; string description = tokens[5]; CompletableLanguageElement elType; if (!Enum.TryParse(langElemText, out elType)) { Utils.DebugPrintToOutput("Failed to parse language element found in racer autocomplete response: {0}", langElemText); continue; } string insertionText = tokenType == RustTokenTypes.STRUCTURAL ? tokenText + text : text; var icon = GetCompletionIcon(elType); yield return(new Completion(text, insertionText, description, icon, null)); } }
/// <summary> /// Fetches auto complete suggestions and appends to the completion sets of the current completion session. /// </summary> /// <param name="session">The active completion session, initiated from the completion command handler.</param> /// <param name="completionSets">A list of completion sets that may be augmented by this source.</param> public void AugmentCompletionSession(ICompletionSession session, IList <CompletionSet> completionSets) { if (disposed) { throw new ObjectDisposedException(GetType().Name); } ITextSnapshot snapshot = buffer.CurrentSnapshot; SnapshotPoint?sp = session.GetTriggerPoint(snapshot); if (!sp.HasValue) { return; } var triggerPoint = sp.Value; var line = triggerPoint.GetContainingLine(); int col = triggerPoint.Position - line.Start.Position; if (line.GetText() == "" || col == 0 || char.IsWhiteSpace(line.GetText()[col - 1])) { // On empty rows or without a prefix, return only completions for rust keywords. var location = snapshot.CreateTrackingSpan(col + line.Start.Position, 0, SpanTrackingMode.EdgeInclusive); completionSets.Add(new RustCompletionSet("All", "All", location, keywordCompletions, null)); return; } // Get token under cursor. var tokens = Utils.LexString(line.GetText()); var activeToken = col == line.Length ? tokens.Last() : tokens.FirstOrDefault(t => col >= t.StartIndex && col <= t.StopIndex); if (activeToken == null) { return; } RustTokenTypes tokenType = Utils.LexerTokenToRustToken(activeToken.Text, activeToken.Type); // Establish the extents of the current token left of the cursor. var extent = new TextExtent( new SnapshotSpan( new SnapshotPoint(snapshot, activeToken.StartIndex + line.Start.Position), triggerPoint), tokenType != RustTokenTypes.WHITESPACE); var span = snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive); // Fetch racer completions & return in a completion set. var completions = GetCompletions(tokenType, activeToken.Text, RunRacer(snapshot, triggerPoint)).ToList(); completions.AddRange(keywordCompletions); completionSets.Add(new RustCompletionSet("All", "All", span, completions, null)); }
public static RustTokenTypes LexerTokenToRustToken(string text, int tok) { RustTokenTypes ty = _tt[tok]; if (ty == RustTokenTypes.IDENT) { if (_kws.Contains(text)) { ty = RustTokenTypes.KEYWORD; } else { ty = RustTokenTypes.IDENT; } } return(ty); }
public static RustTokenTypes LexerTokenToRustToken(string text, int tok) { // FIXME: this is super slow. var _tt = new Dictionary <int, RustTokenTypes>(); _tt[RustLexer.EQ] = RustTokenTypes.OP; _tt[RustLexer.LT] = RustTokenTypes.OP; _tt[RustLexer.LE] = RustTokenTypes.OP; _tt[RustLexer.EQEQ] = RustTokenTypes.OP; _tt[RustLexer.NE] = RustTokenTypes.OP; _tt[RustLexer.GE] = RustTokenTypes.OP; _tt[RustLexer.GT] = RustTokenTypes.OP; _tt[RustLexer.ANDAND] = RustTokenTypes.OP; _tt[RustLexer.OROR] = RustTokenTypes.OP; _tt[RustLexer.NOT] = RustTokenTypes.OP; _tt[RustLexer.TILDE] = RustTokenTypes.OP; _tt[RustLexer.PLUS] = RustTokenTypes.OP; _tt[RustLexer.MINUS] = RustTokenTypes.OP; _tt[RustLexer.STAR] = RustTokenTypes.OP; _tt[RustLexer.SLASH] = RustTokenTypes.OP; _tt[RustLexer.PERCENT] = RustTokenTypes.OP; _tt[RustLexer.CARET] = RustTokenTypes.OP; _tt[RustLexer.AND] = RustTokenTypes.OP; _tt[RustLexer.OR] = RustTokenTypes.OP; _tt[RustLexer.SHL] = RustTokenTypes.OP; _tt[RustLexer.SHR] = RustTokenTypes.OP; _tt[RustLexer.BINOP] = RustTokenTypes.OP; _tt[RustLexer.BINOPEQ] = RustTokenTypes.OP; _tt[RustLexer.AT] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.DOT] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.DOTDOT] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.DOTDOTDOT] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.COMMA] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.SEMI] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.COLON] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.MOD_SEP] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.RARROW] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.FAT_ARROW] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.LPAREN] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.RPAREN] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.LBRACKET] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.RBRACKET] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.LBRACE] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.RBRACE] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.POUND] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.DOLLAR] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.UNDERSCORE] = RustTokenTypes.STRUCTURAL; _tt[RustLexer.LIT_CHAR] = RustTokenTypes.CHAR; _tt[RustLexer.LIT_INTEGER] = RustTokenTypes.NUMBER; _tt[RustLexer.LIT_FLOAT] = RustTokenTypes.NUMBER; _tt[RustLexer.LIT_STR] = RustTokenTypes.STRING; _tt[RustLexer.LIT_STR_RAW] = RustTokenTypes.STRING; _tt[RustLexer.LIT_BINARY] = RustTokenTypes.STRING; _tt[RustLexer.LIT_BINARY_RAW] = RustTokenTypes.STRING; _tt[RustLexer.IDENT] = RustTokenTypes.IDENT; _tt[RustLexer.LIFETIME] = RustTokenTypes.LIFETIME; _tt[RustLexer.WHITESPACE] = RustTokenTypes.WHITESPACE; _tt[RustLexer.DOC_COMMENT] = RustTokenTypes.DOC_COMMENT; _tt[RustLexer.COMMENT] = RustTokenTypes.COMMENT; var _kws = new HashSet <string> { "as", "box", "break", "continue", "crate", "else", "enum", "extern", "false", "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "mut", "priv", "proc", "pub", "ref", "return", "self", "static", "struct", "super", "true", "trait", "type", "unsafe", "use", "while" }; RustTokenTypes ty = _tt[tok]; if (ty == RustTokenTypes.IDENT) { if (_kws.Contains(text)) { ty = RustTokenTypes.KEYWORD; } else { ty = RustTokenTypes.IDENT; } } return(ty); }
public RustTokenTag(RustTokenTypes type) { this.type = type; }