VBLexer GenerateLexerForSnippet(StringReader sr, SnippetType type) { var lexer = ParserFactory.CreateLexer(sr); lexer.SetInitialContext(type); return(lexer); }
static bool TryDeclarationTypeInference(ITextEditor editor, IDocumentLine curLine) { string lineText = editor.Document.GetText(curLine.Offset, curLine.Length); ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new System.IO.StringReader(lineText)); if (lexer.NextToken().Kind != Tokens.Dim) { return(false); } if (lexer.NextToken().Kind != Tokens.Identifier) { return(false); } if (lexer.NextToken().Kind != Tokens.As) { return(false); } Token t1 = lexer.NextToken(); if (t1.Kind != Tokens.QuestionMark) { return(false); } Token t2 = lexer.NextToken(); if (t2.Kind != Tokens.Assign) { return(false); } string expr = lineText.Substring(t2.Location.Column); LoggingService.Debug("DeclarationTypeInference: >" + expr + "<"); ResolveResult rr = ParserService.Resolve(new ExpressionResult(expr), editor.Caret.Line, t2.Location.Column, editor.FileName, editor.Document.Text); if (rr != null && rr.ResolvedType != null) { ClassFinder context = new ClassFinder(ParserService.GetParseInformation(editor.FileName), editor.Caret.Line, t1.Location.Column); VBNetAmbience ambience = new VBNetAmbience(); if (CodeGenerator.CanUseShortTypeName(rr.ResolvedType, context)) { ambience.ConversionFlags = ConversionFlags.None; } else { ambience.ConversionFlags = ConversionFlags.UseFullyQualifiedTypeNames; } string typeName = ambience.Convert(rr.ResolvedType); using (editor.Document.OpenUndoGroup()) { int offset = curLine.Offset + t1.Location.Column - 1; editor.Document.Remove(offset, 1); editor.Document.Insert(offset, typeName); } editor.Caret.Column += typeName.Length - 1; return(true); } return(false); }
ILexer GenerateLexerForSnippet(StringReader sr, SnippetType type) { var lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, sr); lexer.SetInitialContext(type); return(lexer); }
ILexer GenerateLexer(string text) { ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.CSharp, new StringReader(text)); lexer.EvaluateConditionalCompilation = true; lexer.ConditionalCompilationSymbols["TEST"] = null; return(lexer); }
bool IsEndStatementNeeded(TextArea textArea, ref VBStatement statement, int lineNr) { Stack <Token> tokens = new Stack <Token>(); List <Token> missingEnds = new List <Token>(); ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(textArea.Document.TextContent)); Token currentToken = null; Token prevToken = null; while ((currentToken = lexer.NextToken()).Kind != Tokens.EOF) { if (prevToken == null) { prevToken = currentToken; } if (IsBlockStart(lexer, currentToken, prevToken)) { if ((tokens.Count > 0 && tokens.Peek().Kind != Tokens.Interface) || IsDeclaration(currentToken.Kind)) { tokens.Push(currentToken); } } if (IsBlockEnd(currentToken, prevToken)) { while (tokens.Count > 0 && !IsMatchingEnd(tokens.Peek(), currentToken)) { missingEnds.Add(tokens.Pop()); } if (tokens.Count > 0) { if (IsMatchingEnd(tokens.Peek(), currentToken)) { tokens.Pop(); } } } prevToken = currentToken; } while (tokens.Count > 0) { missingEnds.Add(tokens.Pop()); } if (missingEnds.Count > 0) { return(GetClosestMissing(missingEnds, statement, textArea, lineNr) != null); } else { return(false); } }
private bool TryDeclarationTypeInference(CodeEditorControl editor, string curLine) { string lineText = curLine; ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new System.IO.StringReader(lineText)); if (lexer.NextToken().kind != VBTokens.Dim) { return(false); } if (lexer.NextToken().kind != VBTokens.Identifier) { return(false); } if (lexer.NextToken().kind != VBTokens.As) { return(false); } Token t1 = lexer.NextToken(); if (t1.kind != VBTokens.QuestionMark) { return(false); } Token t2 = lexer.NextToken(); if (t2.kind != VBTokens.Assign) { return(false); } string expr = lineText.Substring(t2.col); ResolveResult rr = ScriptControl.Parser.ProjectParser.GetResolver().Resolve(new ExpressionResult(expr), editor.ActiveViewControl.Caret.Position.Y + 1, t2.col, editor.ActiveViewControl.FileName, ScriptControl.Parser.ProjectParser.GetFileContents(editor.FileName)); if (rr != null && rr.ResolvedType != null) { ClassFinder context = new ClassFinder(editor.ActiveViewControl.FileName, editor.ActiveViewControl.Caret.Position.Y, t1.col); if (CodeGenerator.CanUseShortTypeName(rr.ResolvedType, context)) { VBNetAmbience.Instance.ConversionFlags = ConversionFlags.None; } else { VBNetAmbience.Instance.ConversionFlags = ConversionFlags.UseFullyQualifiedNames; } string typeName = VBNetAmbience.Instance.Convert(rr.ResolvedType); int offset = editor.ActiveViewControl.Document.GetRange(new TextRange(0, 0, 0, editor.ActiveViewControl.Caret.Position.Y)).Length; editor.ActiveViewControl.Document.InsertText(typeName, offset + t1.col - 1, editor.ActiveViewControl.Caret.Position.Y); editor.ActiveViewControl.Caret.SetPos(new TextPoint(editor.ActiveViewControl.Caret.Position.X + typeName.Length - 1, editor.ActiveViewControl.Caret.Position.Y)); return(true); } return(false); }
public bool InsightRefreshOnComma(ITextEditor editor, char ch, out IInsightWindow insightWindow) { // Show MethodInsightWindow or IndexerInsightWindow NRefactoryResolver r = new NRefactoryResolver(languageProperties); Location cursorLocation = editor.Caret.Position; if (r.Initialize(ParserService.GetParseInformation(editor.FileName), cursorLocation.Y, cursorLocation.X)) { TextReader currentMethod = r.ExtractCurrentMethod(editor.Document.Text); if (currentMethod != null) { ILexer lexer = ParserFactory.CreateLexer(language, currentMethod); Token token; InspectedCall call = new InspectedCall(Location.Empty, null); call.parent = call; while ((token = lexer.NextToken()) != null && token.Kind != eofToken && token.Location < cursorLocation) { if (token.Kind == commaToken) { call.commas.Add(token.Location); } else if (token.Kind == openParensToken || token.Kind == openBracketToken || token.Kind == openBracesToken) { call = new InspectedCall(token.Location, call); } else if (token.Kind == closeParensToken || token.Kind == closeBracketToken || token.Kind == closeBracesToken) { call = call.parent; } } int offset = LocationToOffset(editor, call.start); if (offset >= 0 && offset < editor.Document.TextLength) { char c = editor.Document.GetCharAt(offset); if (c == '(' || c == '[') { var insightProvider = new MethodInsightProvider { LookupOffset = offset }; var insightItems = insightProvider.ProvideInsight(editor); insightWindow = ShowInsight(editor, insightItems, ResolveCallParameters(editor, call), ch); return(insightWindow != null); } else { Core.LoggingService.Warn("Expected '(' or '[' at start position"); } } } } insightWindow = null; return(false); }
static void GetCommentOrStringState(ITextEditor editor, out bool inString, out bool inComment) { ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, editor.Document.CreateReader()); Token t = lexer.NextToken(); bool inXml = false; inString = false; inComment = false; while (t.Location < editor.Caret.Position) { t = lexer.NextToken(); if (inXml && (t.Kind != Tokens.Identifier && t.Kind != Tokens.LiteralString && !(t.Kind > Tokens.LiteralDate && t.Kind < Tokens.Colon))) { inXml = false; continue; } if (t.Kind > Tokens.LiteralDate && t.Kind < Tokens.Assign) { inXml = true; } } if (inXml) { // TODO } else { string lineText = editor.Document.GetLine(editor.Caret.Line).Text; for (int i = 0; i < editor.Caret.Column - 1; i++) { char ch = lineText[i]; if (!inComment && ch == '"') { inString = !inString; } if (!inString && ch == '\'') { inComment = true; } } } }
static int GetArgumentIndex(ITextEditor editor) { ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, editor.Document.CreateReader()); ExpressionFinder ef = new ExpressionFinder(); Token t = lexer.NextToken(); while (t.Kind != Tokens.EOF && t.Location < editor.Caret.Position) { ef.InformToken(t); t = lexer.NextToken(); } return(ef.ActiveArgument); }
static bool IsInsideInterface(ITextEditor editor, int lineNr) { ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(editor.Document.Text)); Stack <Token> tokens = new Stack <Token>(); Token currentToken = null; Token prevToken = null; while ((currentToken = lexer.NextToken()).Kind != Tokens.EOF) { if (prevToken == null) { prevToken = currentToken; } if (currentToken.EndLocation.Line <= lineNr && IsDeclaration(currentToken.Kind) && IsBlockStart(lexer, currentToken, prevToken)) { tokens.Push(currentToken); } if (currentToken.EndLocation.Line <= lineNr && IsDeclaration(currentToken.Kind) && IsBlockEnd(currentToken, prevToken)) { if (tokens.Count > 0) { tokens.Pop(); } } if (currentToken.EndLocation.Line > lineNr) { break; } prevToken = currentToken; } if (tokens.Count > 0) { return(tokens.Pop().Kind == Tokens.Interface); } return(false); }
private bool TryDeclarationTypeInference(CodeEditorControl editor, string curLine) { string lineText = curLine; ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.CSharp, new System.IO.StringReader(lineText)); Token typeToken = lexer.NextToken(); if (typeToken.kind == CSTokens.Question) { if (lexer.NextToken().kind == CSTokens.Identifier) { Token t = lexer.NextToken(); if (t.kind == CSTokens.Assign) { string expr = lineText.Substring(t.col); //LoggingService.Debug("DeclarationTypeInference: >" + expr + "<"); ResolveResult rr = ScriptControl.Parser.ProjectParser.GetResolver().Resolve(new ExpressionResult(expr), editor.ActiveViewControl.Caret.Position.Y + 1, t.col, editor.ActiveViewControl.FileName, ScriptControl.Parser.ProjectParser.GetFileContents(editor.FileName)); if (rr != null && rr.ResolvedType != null) { ClassFinder context = new ClassFinder(editor.ActiveViewControl.FileName, editor.ActiveViewControl.Caret.Position.Y, t.col); if (CodeGenerator.CanUseShortTypeName(rr.ResolvedType, context)) { CSharpAmbience.Instance.ConversionFlags = ConversionFlags.None; } else { CSharpAmbience.Instance.ConversionFlags = ConversionFlags.UseFullyQualifiedNames; } string typeName = CSharpAmbience.Instance.Convert(rr.ResolvedType); //editor.Document.Replace(curLine.Offset + typeToken.col - 1, 1, typeName); int offset = editor.ActiveViewControl.Document.GetRange(new TextRange(0, 0, 0, editor.ActiveViewControl.Caret.Position.Y)).Length; editor.ActiveViewControl.Document.InsertText(typeName, offset + typeToken.col - 1, editor.ActiveViewControl.Caret.Position.Y); editor.ActiveViewControl.Caret.SetPos(new TextPoint(editor.ActiveViewControl.Caret.Position.X + typeName.Length - 1, editor.ActiveViewControl.Caret.Position.Y)); return(true); } } } } return(false); }
void RunTest(string code, string expectedOutput) { ExpressionFinder p = new ExpressionFinder(); ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(code)); Token t; do { t = lexer.NextToken(); p.InformToken(t); } while (t.Kind != VBParser.Tokens.EOF); Console.WriteLine(p.Output); Assert.IsEmpty(p.Errors); Assert.AreEqual(expectedOutput, p.Output); }
bool TryDeclarationTypeInference(SharpDevelopTextAreaControl editor, LineSegment curLine) { string lineText = editor.Document.GetText(curLine.Offset, curLine.Length); ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.CSharp, new System.IO.StringReader(lineText)); Token typeToken = lexer.NextToken(); if (typeToken.kind == CSTokens.Question) { if (lexer.NextToken().kind == CSTokens.Identifier) { Token t = lexer.NextToken(); if (t.kind == CSTokens.Assign) { string expr = lineText.Substring(t.col); LoggingService.Debug("DeclarationTypeInference: >" + expr + "<"); ResolveResult rr = ParserService.Resolve(new ExpressionResult(expr), editor.ActiveTextAreaControl.Caret.Line + 1, t.col, editor.FileName, editor.Document.TextContent); if (rr != null && rr.ResolvedType != null) { ClassFinder context = new ClassFinder(editor.FileName, editor.ActiveTextAreaControl.Caret.Line, t.col); if (CodeGenerator.CanUseShortTypeName(rr.ResolvedType, context)) { CSharpAmbience.Instance.ConversionFlags = ConversionFlags.None; } else { CSharpAmbience.Instance.ConversionFlags = ConversionFlags.UseFullyQualifiedNames; } string typeName = CSharpAmbience.Instance.Convert(rr.ResolvedType); editor.Document.Replace(curLine.Offset + typeToken.col - 1, 1, typeName); editor.ActiveTextAreaControl.Caret.Column += typeName.Length - 1; return(true); } } } } return(false); }
public void InitializeOpenedInsightWindow(ITextEditor editor, IInsightWindow insightWindow) { EventHandler <TextChangeEventArgs> onDocumentChanged = delegate { // whenever the document is changed, recalculate EndOffset var remainingDocument = editor.Document.CreateReader(insightWindow.StartOffset, editor.Document.TextLength - insightWindow.StartOffset); using (ILexer lexer = ParserFactory.CreateLexer(language, remainingDocument)) { lexer.SetInitialLocation(editor.Document.OffsetToPosition(insightWindow.StartOffset)); Token token; int bracketCount = 0; while ((token = lexer.NextToken()) != null && token.Kind != eofToken) { if (token.Kind == openParensToken || token.Kind == openBracketToken || token.Kind == openBracketToken) { bracketCount++; } else if (token.Kind == closeParensToken || token.Kind == closeBracketToken || token.Kind == closeBracesToken) { bracketCount--; if (bracketCount <= 0) { MarkInsightWindowEndOffset(insightWindow, editor, token.Location); break; } } else if (token.Kind == statementEndToken) { MarkInsightWindowEndOffset(insightWindow, editor, token.Location); break; } } } }; insightWindow.DocumentChanged += onDocumentChanged; insightWindow.SelectedItemChanged += delegate { HighlightParameter(insightWindow, highlightedParameter); }; onDocumentChanged(null, null); }
void Init(string text, int offset) { if (offset < 0 || offset > text.Length) { throw new ArgumentOutOfRangeException("offset", offset, "offset must be between 0 and " + text.Length); } lexer = ParserFactory.CreateLexer(SupportedLanguage.CSharp, new StringReader(text)); lexer.SkipAllComments = true; lineOffsets = new List <int>(); lineOffsets.Add(0); for (int i = 0; i < text.Length; i++) { if (i == offset) { targetPosition = new Location(offset - lineOffsets[lineOffsets.Count - 1] + 1, lineOffsets.Count); } if (text[i] == '\n') { lineOffsets.Add(i + 1); } else if (text[i] == '\r') { if (i + 1 < text.Length && text[i + 1] != '\n') { lineOffsets.Add(i + 1); } } } if (offset == text.Length) { targetPosition = new Location(offset - lineOffsets[lineOffsets.Count - 1] + 1, lineOffsets.Count); } frame = new Frame(); lastToken = Tokens.EOF; }
ILexer GenerateLexer(string s) { return(ParserFactory.CreateLexer(SupportedLanguage.CSharp, new StringReader(s))); }
static int SmartIndentInternal(ITextEditor editor, int begin, int end) { ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(editor.Document.Text)); Stack <string> indentation = new Stack <string>(); indentation.Push(string.Empty); List <int> eols = new List <int>(); bool inInterface = false; bool isMustOverride = false; bool isDeclare = false; bool isDelegate = false; Token currentToken = null; Token prevToken = null; int blockStart = 1; int lambdaNesting = 0; while ((currentToken = lexer.NextToken()).Kind != Tokens.EOF) { if (prevToken == null) { prevToken = currentToken; } if (currentToken.Kind == Tokens.MustOverride) { isMustOverride = true; } if (currentToken.Kind == Tokens.Delegate) { isDelegate = true; } if (currentToken.Kind == Tokens.Declare) { isDeclare = true; } if (currentToken.Kind == Tokens.EOL) { isDelegate = isDeclare = isMustOverride = false; eols.Add(currentToken.Location.Line); } if (IsBlockEnd(currentToken, prevToken)) { // indent the lines inside the block // this is an End-statement // hence we indent from blockStart to the previous line int blockEnd = currentToken.Location.Line - 1; // if this is a lambda end include End-Statement in block // if (lambdaNesting > 0 && (currentToken.Kind == Tokens.Function || currentToken.Kind == Tokens.Sub)) { // blockEnd++; // } ApplyToRange(editor, indentation, eols, blockStart, blockEnd, begin, end); if (lambdaNesting > 0 && (currentToken.Kind == Tokens.Function || currentToken.Kind == Tokens.Sub)) { Unindent(indentation); ApplyToRange(editor, indentation, eols, currentToken.Location.Line, currentToken.Location.Line, begin, end); } if (currentToken.Kind == Tokens.Interface) { inInterface = false; } if (!inInterface && !isMustOverride && !isDeclare && !isDelegate) { Unindent(indentation); if (currentToken.Kind == Tokens.Select) { Unindent(indentation); } } // block start is this line (for the lines between two blocks) blockStart = currentToken.Location.Line; if (lambdaNesting > 0 && (currentToken.Kind == Tokens.Function || currentToken.Kind == Tokens.Sub)) { blockStart++; lambdaNesting--; } } bool isMultiLineLambda; if (IsBlockStart(lexer, currentToken, prevToken, out isMultiLineLambda)) { // indent the lines between the last and this block // this is a Begin-statement // hence we indent from blockStart to the this line int lastVisualLine = FindNextEol(lexer); eols.Add(lastVisualLine); ApplyToRange(editor, indentation, eols, blockStart, lastVisualLine, begin, end); if (isMultiLineLambda && (currentToken.Kind == Tokens.Function || currentToken.Kind == Tokens.Sub)) { lambdaNesting++; int endColumn = currentToken.Location.Column; int startColumn = DocumentUtilitites.GetWhitespaceAfter(editor.Document, editor.Document.GetLine(lastVisualLine).Offset).Length; if (startColumn < endColumn) { Indent(editor, indentation, new string(' ', endColumn - startColumn - 1)); } } if (!inInterface && !isMustOverride && !isDeclare && !isDelegate) { Indent(editor, indentation); if (currentToken.Kind == Tokens.Select) { Indent(editor, indentation); } } if (currentToken.Kind == Tokens.Interface) { inInterface = true; } // block start is the following line (for the lines inside a block) blockStart = lastVisualLine + 1; } prevToken = currentToken; } ApplyToRange(editor, indentation, eols, blockStart, editor.Document.TotalNumberOfLines, begin, end); return((indentation.PeekOrDefault() ?? string.Empty).Length); }
protected bool InsightRefreshOnComma(CodeEditorControl editor, char ch) { // Show MethodInsightWindow or IndexerInsightWindow NRefactoryResolver r = new NRefactoryResolver(ScriptControl.Parser.ProjectParser.CurrentProjectContent, languageProperties); Location cursorLocation = new Location(editor.ActiveViewControl.Caret.Position.X + 1, editor.ActiveViewControl.Caret.Position.Y + 1); if (r.Initialize(editor.ActiveViewControl.FileName, cursorLocation.Y, cursorLocation.X)) { TextReader currentMethod = r.ExtractCurrentMethod(ScriptControl.Parser.ProjectParser.GetFileContents(editor.FileName)); if (currentMethod != null) { ILexer lexer = ParserFactory.CreateLexer(language, currentMethod); Token token; InspectedCall call = new InspectedCall(Location.Empty, null); call.parent = call; while ((token = lexer.NextToken()) != null && token.kind != eofToken && token.Location < cursorLocation) { if (token.kind == commaToken) { call.commas.Add(token.Location); } else if (token.kind == openParensToken || token.kind == openBracketToken || token.kind == openBracesToken) { call = new InspectedCall(token.Location, call); } else if (token.kind == closeParensToken || token.kind == closeBracketToken || token.kind == closeBracesToken) { call = call.parent; } } int offset = LocationToOffset(editor, call.start); string docText = ScriptControl.Parser.ProjectParser.GetFileContents(editor.FileName); offset = docText.LastIndexOf('('); //int offset = editor.ActiveViewControl.Document.PointToIntPos(new TextPoint(call.start.X,call.start.Y));//, call.start); if (offset >= 0 && offset < docText.Length) { char c = (char)docText.Substring(offset, 1).ToCharArray(0, 1)[0]; if (c == '(') { ShowInsight(editor, new MethodInsightDataProvider(offset, true), ResolveCallParameters(editor, call), ch); return(true); } else if (c == '[') { ShowInsight(editor, new IndexerInsightDataProvider(offset, true), ResolveCallParameters(editor, call), ch); return(true); } else { //LoggingService.Warn("Expected '(' or '[' at start position"); } } } } return(false); }
int SmartIndentInternal(TextArea textArea, int begin, int end) { ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(textArea.Document.TextContent)); Stack <string> indentation = new Stack <string>(); indentation.Push(string.Empty); int oldLine = 0; bool inInterface = false; bool isMustOverride = false; bool isDeclare = false; bool isDelegate = false; Token currentToken = null; Token prevToken = null; while ((currentToken = lexer.NextToken()).Kind != Tokens.EOF) { if (prevToken == null) { prevToken = currentToken; } if (currentToken.Kind == Tokens.MustOverride) { isMustOverride = true; } if (currentToken.Kind == Tokens.Delegate) { isDelegate = true; } if (currentToken.Kind == Tokens.Declare) { isDeclare = true; } if (currentToken.Kind == Tokens.EOL) { isDelegate = isDeclare = isMustOverride = false; } if (IsBlockEnd(currentToken, prevToken)) { ApplyToRange(textArea, indentation, oldLine, currentToken.Location.Line - 1, begin, end); if (currentToken.Kind == Tokens.Interface) { inInterface = false; } if (!inInterface && !isMustOverride && !isDeclare && !isDelegate) { Unindent(indentation); if (currentToken.Kind == Tokens.Select) { Unindent(indentation); } } oldLine = currentToken.Location.Line - 1; } if (IsBlockStart(lexer, currentToken, prevToken)) { int line = GetLastVisualLine(currentToken.Location.Line, textArea); ApplyToRange(textArea, indentation, oldLine, line, begin, end); if (!inInterface && !isMustOverride && !isDeclare && !isDelegate) { Indent(textArea, indentation); if (currentToken.Kind == Tokens.Select) { Indent(textArea, indentation); } } if (currentToken.Kind == Tokens.Interface) { inInterface = true; } oldLine = line; } prevToken = currentToken; } // do last indent step int newLine = prevToken.Location.Line; if (oldLine > newLine) { newLine = oldLine + 1; } ApplyToRange(textArea, indentation, oldLine, newLine, begin, end); return((indentation.PeekOrDefault() ?? string.Empty).Length); }
public ExpressionResult FindFullExpression(string text, int offset) { Init(text, offset); ExpressionFinder p = new ExpressionFinder(); lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(text)); Token t; Block block = Block.Default; var expressionDelimiters = new[] { Tokens.EOL, Tokens.Colon, Tokens.Dot, Tokens.TripleDot, Tokens.DotAt }; while (true) { t = lexer.NextToken(); p.InformToken(t); if (block == Block.Default && t.EndLocation > targetPosition) { block = p.CurrentBlock; } if (block != Block.Default && (block.isClosed || expressionDelimiters.Contains(t.Kind) && block == p.CurrentBlock)) { break; } if (t.Kind == Tokens.EOF) { break; } } if (p.Errors.Any()) { foreach (var e in p.Errors) { LoggingService.Warn("not expected: " + e); } } BitArray expectedSet; try { expectedSet = p.GetExpectedSet(); } catch (InvalidOperationException) { expectedSet = null; } int tokenOffset; if (t == null || t.Kind == Tokens.EOF) { tokenOffset = text.Length; } else { tokenOffset = LocationToOffset(t.Location); } int lastExpressionStartOffset = LocationToOffset(block.lastExpressionStart); if (lastExpressionStartOffset >= 0) { if (offset < tokenOffset) { // offset is in front of this token return(MakeResult(text, lastExpressionStartOffset, tokenOffset, GetContext(block), expectedSet)); } else { // offset is IN this token return(MakeResult(text, lastExpressionStartOffset, offset, GetContext(block), expectedSet)); } } else { return(new ExpressionResult(null, GetContext(block))); } }
public ExpressionResult FindExpression(string text, int offset) { Init(text, offset); ExpressionFinder p = new ExpressionFinder(); lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(text)); Token t = lexer.NextToken(); // put all tokens in front of targetPosition into the EF-Parser while (t.EndLocation < targetPosition) { p.InformToken(t); t = lexer.NextToken(); } // put current token into EF-Parser if it cannot be continued (is simple operator) if (t.EndLocation == targetPosition && ((t.Kind <= Tokens.ColonAssign && t.Kind > Tokens.Identifier) || t.Kind == Tokens.EOL)) { p.InformToken(t); t = lexer.NextToken(); } // make sure semantic actions are executed p.Advance(); // remember current state, we'll use it to determine the context var block = p.CurrentBlock; ExpressionContext context = p.IsIdentifierExpected && !p.IsMissingModifier ? ExpressionContext.IdentifierExpected : GetContext(block); BitArray expectedSet; try { expectedSet = p.GetExpectedSet(); } catch (InvalidOperationException) { expectedSet = null; } // put current token into EF-Parser if (t.Location < targetPosition) { p.InformToken(t); } if (p.Errors.Any()) { foreach (var e in p.Errors) { LoggingService.Warn("not expected: " + e); } } if (p.NextTokenIsPotentialStartOfExpression) { return(new ExpressionResult("", new DomRegion(targetPosition.Line, targetPosition.Column), context, expectedSet)); } int lastExpressionStartOffset = LocationToOffset(p.CurrentBlock.lastExpressionStart); if (lastExpressionStartOffset < 0) { return(new ExpressionResult("", new DomRegion(targetPosition.Line, targetPosition.Column), context, expectedSet)); } return(MakeResult(text, lastExpressionStartOffset, offset, context, expectedSet)); }
static int FindBeginStatement(IDocument document, VBStatement statement, Location endLocation, out int length) { ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, document.CreateReader()); Token currentToken = null; Token prevToken = null; int lookFor = statement.StatementToken; Stack <Token> tokens = new Stack <Token>(); if (statement.EndStatement == "Next") { lookFor = Tokens.For; } Token result = null; // Token firstModifier = null; while ((currentToken = lexer.NextToken()).Kind != Tokens.EOF) { if (prevToken == null) { prevToken = currentToken; } // if (IsModifier(currentToken)) { // if (firstModifier == null) // firstModifier = currentToken; // } else // firstModifier = null; if (VBNetFormattingStrategy.IsBlockStart(lexer, currentToken, prevToken)) { tokens.Push(currentToken); } if (VBNetFormattingStrategy.IsBlockEnd(currentToken, prevToken)) { while (tokens.Count > 0 && !VBNetFormattingStrategy.IsMatchingEnd(tokens.Peek(), currentToken)) { tokens.Pop(); } if (tokens.Count > 0) { Token t = tokens.Pop(); if (currentToken.Location.Line == endLocation.Line) { result = t; break; } } } prevToken = currentToken; } if (result != null) { int endOffset = document.PositionToOffset(result.EndLocation.Line, result.EndLocation.Column); int offset = document.PositionToOffset(result.Location.Line, result.Location.Column); length = endOffset - offset; return(offset); } length = 0; return(-1); }
protected bool InsightRefreshOnComma(SharpDevelopTextAreaControl editor, char ch) { // Show MethodInsightWindow or IndexerInsightWindow NRefactoryResolver r = new NRefactoryResolver(ParserService.CurrentProjectContent, languageProperties); Location cursorLocation = new Location(editor.ActiveTextAreaControl.Caret.Column + 1, editor.ActiveTextAreaControl.Caret.Line + 1); if (r.Initialize(editor.FileName, cursorLocation.Y, cursorLocation.X)) { TextReader currentMethod = r.ExtractCurrentMethod(editor.Text); if (currentMethod != null) { ILexer lexer = ParserFactory.CreateLexer(language, currentMethod); Token token; InspectedCall call = new InspectedCall(Location.Empty, null); call.parent = call; while ((token = lexer.NextToken()) != null && token.kind != eofToken && token.Location < cursorLocation) { if (token.kind == commaToken) { call.commas.Add(token.Location); } else if (token.kind == openParensToken || token.kind == openBracketToken || token.kind == openBracesToken) { call = new InspectedCall(token.Location, call); } else if (token.kind == closeParensToken || token.kind == closeBracketToken || token.kind == closeBracesToken) { call = call.parent; } } int offset = LocationToOffset(editor, call.start); if (offset >= 0 && offset < editor.Document.TextLength) { char c = editor.Document.GetCharAt(offset); if (c == '(') { ShowInsight(editor, new MethodInsightDataProvider(offset, true), ResolveCallParameters(editor, call), ch); return(true); } else if (c == '[') { ShowInsight(editor, new IndexerInsightDataProvider(offset, true), ResolveCallParameters(editor, call), ch); return(true); } else { LoggingService.Warn("Expected '(' or '[' at start position"); } } } } return(false); }
VBLexer GenerateLexer(StringReader sr) { return(ParserFactory.CreateLexer(sr)); }
public bool InsightRefreshOnComma(ITextEditor editor, char ch, out IInsightWindow insightWindow) { // Show MethodInsightWindow or IndexerInsightWindow NRefactoryResolver r = new NRefactoryResolver(languageProperties); Location cursorLocation = editor.Caret.Position; if (r.Initialize(ParserService.GetParseInformation(editor.FileName), cursorLocation.Y, cursorLocation.X)) { TextReader currentMethod = r.ExtractCurrentMethod(editor.Document.Text); if (currentMethod != null) { ILexer lexer = ParserFactory.CreateLexer(language, currentMethod); Token token; InspectedCall call = new InspectedCall(Location.Empty, null); call.parent = call; // HACK MINI PARSER // The following code tries to find the current nested call until the caret position (= cursorLocation) is // reached. call.commas contains all commas up to the caret position. // DOES NOT HANDLE GENERICS CORRECTLY! This is sufficient for overload "search", because if we miss one // overload it does not matter. But if we highlight the wrong parameter (see below) it DOES MATTER! while ((token = lexer.NextToken()) != null && token.Kind != eofToken && token.Location < cursorLocation) { if (token.Kind == commaToken) { call.commas.Add(token.Location); } else if (token.Kind == openParensToken || token.Kind == openBracketToken || token.Kind == openBracesToken) { call = new InspectedCall(token.Location, call); } else if (token.Kind == closeParensToken || token.Kind == closeBracketToken || token.Kind == closeBracesToken) { call = call.parent; } } int offset = LocationToOffset(editor, call.start); if (offset >= 0 && offset < editor.Document.TextLength) { char c = editor.Document.GetCharAt(offset); if (c == '(' || c == '[') { var insightProvider = new MethodInsightProvider { LookupOffset = offset }; var insightItems = insightProvider.ProvideInsight(editor); // find highlighted parameter // see mini parser description above; the number of recognized parameters is the index // of the current parameter! var parameters = ResolveCallParameters(editor, call); highlightedParameter = parameters.Count; insightWindow = ShowInsight(editor, insightItems, parameters, ch); return(insightWindow != null); } else { LoggingService.Warn("Expected '(' or '[' at start position"); } } } } insightWindow = null; return(false); }
ILexer GenerateLexer(StringReader sr) { return(ParserFactory.CreateLexer(SupportedLanguage.VBNet, sr)); }
VBLexer GenerateLexer(string s) { return(ParserFactory.CreateLexer(new StringReader(s))); }