/// <summary> /// Handles the causing parse-request. /// </summary> /// <param name="req">The parse-request.</param> protected virtual void HandleRequest(ParseRequest req) { // use full source text was in e.g. productions, we wan't to suggest productions which were specfied after the current position string sourceText = req.Text; if (string.IsNullOrEmpty(sourceText)) //HACK: empty source text makes problems for coco, so supply at least a whitespace { sourceText = " "; } CocoParseResult parseResult; // we could do something like auto-parse when the source is parsed, highlight matched braces, get members, get quick info // but we are only interested in code completions switch (req.Reason) { case ParseReason.CompleteWord: parseResult = CocoParserProxy.Parse(sourceText, req.Line, req.Col, Source.GetFilePath()); //this sets the possible declarations CreateDeclarations(parseResult); break; case ParseReason.Goto: parseResult = CocoParserProxy.Parse(sourceText, Source.GetFilePath()); //get selected text (text of selected token) TokenInfo info = Source.GetTokenInfo(req.Line, req.Col); string selectedText = Source.GetText(req.Line, info.StartIndex, req.Line, info.EndIndex + 1); //look if there is a tokeninfo for this selected text AddTokenInfo addInfo = FindTokenInfo(parseResult, selectedText); TextSpan span = new TextSpan(); //when there is a tokeninfo for it, set the span where to navigate within the current file if (addInfo != null) { int length = addInfo.Name == null ? 0 : addInfo.Name.Length; span = new TextSpan(); span.iStartLine = span.iEndLine = addInfo.Line - 1; span.iStartIndex = addInfo.Col - 1; span.iEndIndex = addInfo.Col - 1 + length; SourceSpanValid = true; } else { SourceSpanValid = false; } SourceSpan = span; break; default: break; } }
private AddTokenInfo FindTokenInfo(CocoParserProxy.CocoParseResult result, string text) { AddTokenInfo info = null; //search first in productions, then in tokens, then in charsets info = result.Productions.FirstOrDefault(n => n.Name == text); if (info != null) { return(info); } info = result.Tokens.FirstOrDefault(n => n.Name == text); if (info != null) { return(info); } info = result.CharSets.FirstOrDefault(n => n.Name == text); return(info); }