/// <summary> /// Check if the property name in the text buffer has changed. /// If so, then dismiss the syntax help tip. /// </summary> private void OnTextBufferChanged(object sender, TextContentChangedEventArgs eventArgs) { if (_trackingSpan != null && _session != null) { ITextSnapshot snapshot = _trackingSpan.TextBuffer.CurrentSnapshot; SnapshotPoint startPoint = _trackingSpan.GetStartPoint(snapshot); bool propertyNameStillValid = false; if (startPoint.Position + _propertyName.Length <= snapshot.Length) { // Get the current text at the beginning of the tracking span. string text = snapshot.GetText(startPoint.Position, _propertyName.Length); char afterText = (startPoint.Position + _propertyName.Length < snapshot.Length) ? snapshot.GetText(startPoint.Position + _propertyName.Length, 1)[0] : '\0'; if (string.Equals(text, _propertyName, StringComparison.OrdinalIgnoreCase) && !char.IsLetterOrDigit(afterText) && afterText != '-') { // The correct property name is still in the code propertyNameStillValid = true; } } if (!propertyNameStillValid) { _session.Dismiss(); } } }
private void VerifyTags(ITextSnapshot snapshot, IEnumerable <ITagSpan <IOutliningRegionTag> > tags, params ExpectedTag[] expected) { var ltags = new List <ITagSpan <IOutliningRegionTag> >(tags); // Print this out so we can easily update the tests if things change. foreach (var tag in ltags) { int start = tag.Span.Start.Position; int end = tag.Span.End.Position; Console.WriteLine("new ExpectedTag({0}, {1}, \"{2}\"),", start, end, Classification.FormatString(snapshot.GetText(Span.FromBounds(start, end))) ); } Assert.AreEqual(expected.Length, ltags.Count); for (int i = 0; i < ltags.Count; i++) { int start = ltags[i].Span.Start.Position; int end = ltags[i].Span.End.Position; Assert.AreEqual(expected[i].Start, start); Assert.AreEqual(expected[i].End, end); Assert.AreEqual(expected[i].Text, snapshot.GetText(Span.FromBounds(start, end))); Assert.AreEqual(ltags[i].Tag.IsImplementation, true); } }
private void UpdateCachedBlock(int position, int length) { if (!_partial) { if (_cachedBlock == null) { _cachedBlock = _snapshot.GetText(0, _snapshot.Length); } } else { if (_cachedBlock == null || position < _basePosition || (_basePosition + _cachedBlock.Length < position + length)) { if (position < DefaultPreBlockLength) { length += position; position = 0; } else { length += DefaultPreBlockLength; position -= DefaultPreBlockLength; } length = Math.Max(length, _partialBlockLength); length = Math.Min(length, _snapshot.Length - position); _cachedBlock = _snapshot.GetText(position, length); _basePosition = position; } } }
bool ParseAndTypeCheck(bool runResolver) { Tuple <ITextSnapshot, Dafny.Program, List <DafnyError> > parseResult; Dafny.Program program; var errorReporter = new VSErrorReporter(this); if (_buffer.Properties.TryGetProperty(bufferDafnyKey, out parseResult) && (parseResult.Item1 == _snapshot)) { // already parsed; program = parseResult.Item2; _errors = parseResult.Item3; if (program == null) { runResolver = false; } } else { Dafny.ModuleDecl module = new Dafny.LiteralModuleDecl(new Dafny.DefaultModuleDecl(), null); Dafny.BuiltIns builtIns = new Dafny.BuiltIns(); var parseErrors = new Dafny.Errors(errorReporter); int errorCount = Dafny.Parser.Parse(_snapshot.GetText(), _filename, _filename, null, module, builtIns, parseErrors); /* (_snapshot.GetText(), _filename, _filename, module, builtIns, parseErrors);*/ string errString = Dafny.Main.ParseIncludes(module, builtIns, new List <string>(), parseErrors); if (errorCount != 0 || errString != null) { runResolver = false; program = null; } else { program = new Dafny.Program(_filename, module, builtIns, errorReporter); } _buffer.Properties[bufferDafnyKey] = new Tuple <ITextSnapshot, Dafny.Program, List <DafnyError> >(_snapshot, program, _errors); } if (!runResolver) { return(false); } program.Raw = _snapshot.GetText(); var r = new Resolver(program); r.ResolveProgram(program); MostRecentResolver = r; if (errorReporter.Count(ErrorLevel.Error) != 0) { return(false); } _program = program; return(true); // success }
// parse the document from the start, and try to // figure out where the opening tag matching our closing tag starts private SnapshotSpan?FindOpeningTag(ITextSnapshot snapshot, int searchEnd, string searchFor) { String textToSearch = snapshot.GetText(0, searchEnd); int origLineNum = snapshot.GetLineNumberFromPosition(searchEnd); using (SgmlReader reader = new SgmlReader()) { reader.InputStream = new StringReader(textToSearch); reader.WhitespaceHandling = WhitespaceHandling.All; try { Stack <int> openingPositions = new Stack <int>(); while (reader.Read()) { if (reader.LocalName != searchFor) { continue; } if (reader.NodeType == XmlNodeType.Element && !reader.IsEmptyElement) { // find close to where the tag starts int lineNum = reader.LineNumber - 1; var line = snapshot.GetLineFromLineNumber(lineNum); int position = line.Start.Position + reader.LinePosition - searchFor.Length; position = BacktrackToLessThan(snapshot, position); String textFound = snapshot.GetText(position, 10); openingPositions.Push(position); } else if (reader.NodeType == XmlNodeType.EndElement) { if (openingPositions.Count <= 0) { // document is malformed, so just get the heck out return(null); } var line = snapshot.GetLineFromLineNumber(reader.LineNumber - 1); int position = line.Start.Position + reader.LinePosition; if (position >= searchEnd) { break; } openingPositions.Pop(); } } // done, last if (openingPositions.Count > 0) { int position = openingPositions.Pop(); return(new SnapshotSpan(snapshot, position, searchFor.Length + 2)); } } catch (Exception ex) { Trace.WriteLine(String.Format("Exception while parsing document: {0}.", ex.ToString())); } } return(null); }
public ITextSnapshot Delete(Span deleteSpan) { var sb = new StringBuilder(); var oldText = CurrentSnapshot.GetText(deleteSpan.Start, deleteSpan.Length); sb.Append(CurrentSnapshot.GetText(0, deleteSpan.Start)); sb.Append(CurrentSnapshot.GetText(deleteSpan.End, CurrentSnapshot.Length - deleteSpan.End)); TextChangeMock change = new TextChangeMock(deleteSpan.Start, deleteSpan.Length, oldText, String.Empty); TextSnapshotMock nextSnapshot = ((TextSnapshotMock)CurrentSnapshot).CreateNextSnapshot(sb.ToString(), change); ApplyChange(nextSnapshot); return(CurrentSnapshot); }
private void AddOutline(int startLine, int endLine, string collapseText, string hoverText = null) { int startPosition = textSnapshot.GetLineFromLineNumber(startLine).Start; int endPosition = textSnapshot.GetLineFromLineNumber(endLine).End; var span = new Span(startPosition, endPosition - startPosition); if (hoverText == null) { hoverText = textSnapshot.GetText(span); } CurrentFileBlockBuilder.OutliningRegions.Add(new TagSpan <IOutliningRegionTag>( new SnapshotSpan(textSnapshot, span), new OutliningRegionTag(false, false, collapseText, hoverText))); }
public IEnumerable <ITagSpan <IOutliningRegionTag> > GetTags(NormalizedSnapshotSpanCollection spans) { if (spans.Count == 0) { yield break; } ITextSnapshot currentSnapshot = this.currentSnapshot; SnapshotSpan entire = new SnapshotSpan(spans[0].Start, spans[spans.Count - 1].End) .TranslateTo(currentSnapshot, SpanTrackingMode.EdgeExclusive); int startLineNumber = entire.Start.GetContainingLine().LineNumber; int endLineNumber = entire.End.GetContainingLine().LineNumber + 1; // Add 1 to include the line immediately before the brace (that's where the "+" is) foreach (var region in this.RegionParser.CurlyBraceRegions) { if (region.StartLineNumber <= endLineNumber && region.EndLineNumber >= startLineNumber) { // If the startLine is whitespace-only before the bracket, then the region should start at the end of the preceding line. var startLine = currentSnapshot.GetLineFromLineNumber(region.StartLineNumber); var startOffset = region.StartOffset; if (string.IsNullOrWhiteSpace(startLine.GetText().Substring(0, region.StartOffset)) && region.StartLineNumber > 0) { startLine = currentSnapshot.GetLineFromLineNumber(region.StartLineNumber - 1); startOffset = startLine.GetText().TrimEnd().Length; } var endLine = currentSnapshot.GetLineFromLineNumber(region.EndLineNumber); var outlineSpan = new SnapshotSpan(startLine.Start + startOffset, endLine.Start + region.EndOffset); // Get the contents of the tooltip for the collapsed span. const int maxLines = 20; string getTruncatedText() { var truncatedEndLine = currentSnapshot.GetLineFromLineNumber(region.StartLineNumber + maxLines); var truncatedSpan = new SnapshotSpan(startLine.Start, truncatedEndLine.Start + truncatedEndLine.Length); return(currentSnapshot.GetText(truncatedSpan) + Environment.NewLine + ellipsis); } // The hoverText starts at the beginning of StartLine and ends at the end of EndLine. var hoverText = region.EndLineNumber - region.StartLineNumber <= maxLines ? currentSnapshot.GetText(new SnapshotSpan(startLine.Start, endLine.Start + region.EndOffset)) : getTruncatedText(); yield return(new TagSpan <IOutliningRegionTag>(outlineSpan, new OutliningRegionTag(false, false, ellipsis, hoverText))); } } }
/// <summary> /// Gets the tokens by parsing the text snapshot, optionally including any errors. /// </summary> /// <param name = "textSnapshot">The text snapshot.</param> /// <param name = "includeErrors">if set to <c>true</c> [include errors].</param> /// <returns></returns> protected override IEnumerable <IniToken> GetTokens(ITextSnapshot textSnapshot, bool includeErrors) { string text = textSnapshot.GetText(); IEnumerable <IniToken> tokens = Parser.Parse(text); return(tokens); }
internal static AntlrParseResultEventArgs ParseSnapshot(ITextSnapshot snapshot) { Stopwatch timer = Stopwatch.StartNew(); ITokenSource tokenSource = new GrammarLexer(new AntlrInputStream(snapshot.GetText())); CommonTokenStream tokenStream = new CommonTokenStream(tokenSource); GrammarParser.GrammarSpecContext parseResult; GrammarParser parser = new GrammarParser(tokenStream); List<ParseErrorEventArgs> errors = new List<ParseErrorEventArgs>(); try { parser.Interpreter.PredictionMode = PredictionMode.Sll; parser.RemoveErrorListeners(); parser.BuildParseTree = true; parser.ErrorHandler = new BailErrorStrategy(); parseResult = parser.grammarSpec(); } catch (ParseCanceledException ex) { if (!(ex.InnerException is RecognitionException)) throw; tokenStream.Reset(); parser.Interpreter.PredictionMode = PredictionMode.Ll; //parser.AddErrorListener(DescriptiveErrorListener.Default); parser.SetInputStream(tokenStream); parser.ErrorHandler = new DefaultErrorStrategy(); parseResult = parser.grammarSpec(); } return new AntlrParseResultEventArgs(snapshot, errors, timer.Elapsed, tokenStream.GetTokens(), parseResult); }
private void ClassifyBuffer(ITextSnapshot snapshot) { if (disableSyntaxHighlighting) { return; } Debug("Starting classify at {0}, version {1}", DateTime.Now, snapshot.Version.ToString()); ITokenStream tokens; tokens = _sourceWalker.Lex(snapshot.GetText()); lock (gate) { _snapshot = snapshot; _tokens = tokens; XSharpTokens xTokens = new XSharpTokens((BufferedTokenStream)tokens, snapshot); if (_buffer.Properties.ContainsProperty(typeof(XSharpTokens))) { _buffer.Properties.RemoveProperty(typeof(XSharpTokens)); } _buffer.Properties.AddProperty(typeof(XSharpTokens), xTokens); } BuildColorClassifications(tokens, snapshot); Debug("Ending classify at {0}, version {1}", DateTime.Now, snapshot.Version.ToString()); return; }
private ISemanticModel GetSemanticModel(ITextBuffer buffer) { if (currentSnapshot == buffer.CurrentSnapshot) { return(semanticModelCache); } currentSnapshot = buffer.CurrentSnapshot; var code = currentSnapshot.GetText(); if (buffer.ContentType.TypeName == Constants.TstContentType) { semanticModelCache = new SemanticModel(shadowClass); _codeLexerTst.Tokenize(semanticModelCache, code, GetProjectItem(buffer)); _templateLexerTst.Tokenize(semanticModelCache, code, buffer.ContentType); } else { semanticModelCache = new TstXSemanticModel(shadowClass); _codeLexerTstX.Tokenize(semanticModelCache, code, GetProjectItem(buffer)); _templateLexerTstX.Tokenize(semanticModelCache, code, buffer.ContentType); } return(semanticModelCache); }
public static void Update(string file, ITextSnapshot snapshot) { var snapshotText = snapshot.GetText(); bool alreadyCached = false; lock (CacheLock) { alreadyCached = Cache.ContainsKey(file) && Cache[file].RawText == snapshotText; } if (!alreadyCached) { if (!CurrentlyProcessing.Contains(snapshotText)) { try { CurrentlyProcessing.Add(snapshotText); var doc = RapidXamlDocument.Create(snapshot, file, vsa); lock (CacheLock) { Cache[file] = doc; } Parsed?.Invoke(null, new RapidXamlParsingEventArgs(doc, file, snapshot, ParsedAction.Update)); } finally { CurrentlyProcessing.Remove(snapshotText); } } } }
internal static string CheckEncoding(ITextSnapshot snapshot, Encoding documentEncoding, out string magicEncodingName, out int magicEncodingIndex) { var chunk = snapshot.GetText(new Span(0, Math.Min(snapshot.Length, 512))); Parser.GetEncodingFromMagicDesignator(chunk, out var encoding, out magicEncodingName, out magicEncodingIndex); string message = null; if (encoding != null) { // Encoding is specified and is a valid name. // Check if it matches encoding set on the document text buffer. if (encoding.EncodingName != documentEncoding.EncodingName) { message = Strings.WarningEncodingMismatch.FormatUI(documentEncoding.EncodingName, magicEncodingName); } } else { if (!string.IsNullOrEmpty(magicEncodingName)) { // Encoding is specified but not recognized as a valid name message = Strings.WarningInvalidEncoding.FormatUI(magicEncodingName); } else { // Encoding is not specified. Python assumes UTF-8 so we need to verify it. if (Encoding.UTF8.EncodingName != documentEncoding.EncodingName) { message = Strings.WarningEncodingDifferentFromDefault.FormatUI(documentEncoding.EncodingName); } } } return(message); }
private static SyntaxConcept[] Parse(ITextSnapshot snapshot, out bool success) { var sb = new StringBuilder(); sb.Append("format=json tokens="); var dsl = snapshot.GetText(); sb.Append(Encoding.UTF8.GetByteCount(dsl)); var either = Compiler.CompileDsl(sb, null, dsl, cms => (ParseResult)Serializer.ReadObject(cms)); if (!either.Success) { success = false; Parsed(snapshot, new ParsedArgs(either.Error)); return new SyntaxConcept[0]; } var result = either.Value; if (result.Error != null) { var msg = (result.Error.Line >= 0 ? "Line: " + result.Error.Line + ". " : string.Empty) + result.Error.Error; Parsed(snapshot, new ParsedArgs(msg)); } else Parsed(snapshot, new ParsedArgs()); success = result.Error == null; if (result.Tokens == null) return EmptyResult; return result.Tokens.ToArray(); }
/// <summary> /// Checks if a tag's header is visible /// </summary> /// <param name="start">Start position of code block</param> /// <param name="end">End position of code block</param> /// <param name="visibleSpan">the visible span in the textview</param> /// <param name="snapshot">reference to text snapshot. Used for caret check</param> /// <returns>true if the tag is visible (or if all tags are shown)</returns> private bool IsTagVisible(int start, int end, Span?visibleSpan, ITextSnapshot snapshot) { var isVisible = false; // Check general condition if (CBETagPackage.CBEVisibilityMode == (int)CBEOptionPage.VisibilityModes.Always || !visibleSpan.HasValue) { isVisible = true; } // Check visible span if (!isVisible) { var val = visibleSpan.Value; isVisible = (start < val.Start && end >= val.Start && end <= val.End); } // Check if caret is in this line if (isVisible && _TextView != null) { var caretIndex = _TextView.Caret.Position.BufferPosition.Position; var lineStart = Math.Min(caretIndex, end); var lineEnd = Math.Max(caretIndex, end); if (lineStart == lineEnd) { return(false); } else if (lineStart >= 0 && lineEnd <= snapshot.Length) { var line = snapshot.GetText(lineStart, lineEnd - lineStart); return(line.Contains('\n')); } } return(isVisible); }
internal static AntlrParseResultEventArgs ParseSnapshot(ITextSnapshot snapshot) { Stopwatch timer = Stopwatch.StartNew(); ITokenSource tokenSource = new GrammarLexer(new AntlrInputStream(snapshot.GetText())); CommonTokenStream tokenStream = new CommonTokenStream(tokenSource); GrammarParser.GrammarSpecContext parseResult; GrammarParser parser = new GrammarParser(tokenStream); List <ParseErrorEventArgs> errors = new List <ParseErrorEventArgs>(); try { parser.Interpreter.PredictionMode = PredictionMode.Sll; parser.RemoveErrorListeners(); parser.BuildParseTree = true; parser.ErrorHandler = new BailErrorStrategy(); parseResult = parser.grammarSpec(); } catch (ParseCanceledException ex) when(ex.InnerException is RecognitionException) { tokenStream.Reset(); parser.Interpreter.PredictionMode = PredictionMode.Ll; //parser.AddErrorListener(DescriptiveErrorListener.Default); parser.SetInputStream(tokenStream); parser.ErrorHandler = new DefaultErrorStrategy(); parseResult = parser.grammarSpec(); } return(new AntlrParseResultEventArgs(snapshot, errors, timer.Elapsed, tokenStream.GetTokens(), parseResult)); }
private void BackgroundCompile() { ITextBuffer textBuffer = this.TextBuffer; if (textBuffer == null) { return; } ITextSnapshot textSnapshot = textBuffer.CurrentSnapshot; if (this.backgroundCompilationSnapshot.Changed(textSnapshot)) { string filePath = this.FilePath; string sourceText = textSnapshot.GetText(); var versionBefore = textSnapshot.Version; var cancellationToken = this.cancellationTokenSource.Token; var compilation = this.provider.Compile(filePath, sourceText, cancellationToken); if (this.backgroundCompilationSnapshot == null || compilation != null) { textSnapshot = textBuffer.CurrentSnapshot; var versionAfter = textSnapshot.Version; if (versionAfter == versionBefore) { var symbolTokens = this.provider.GetSymbolTokens(compilation, cancellationToken); Interlocked.Exchange(ref this.backgroundCompilationSnapshot, this.backgroundCompilationSnapshot.Update(textSnapshot, compilation, symbolTokens)); } } } }
public static List <ProjectionInfo> GetProjectionInfos(ITextSnapshot snapshot) { List <ProjectionInfo> projections = new List <ProjectionInfo>(); var scriptRegex = new Regex(@"(?i)(<script[^>]*>(?<JavaScript>[^<]+)</script>) |(?i)(<dot:InlineScript[^>]*>(?<JavaScript>[^<]+)</dot:InlineScript>) |(?i)(<dot:InlineTypeScript[^>]*>(?<TypeScript>[^<]+)</dot:InlineTypeScript>) |(?i)(<dot:InlineStyle[^>]*>(?<css>[^<]+)</dot:InlineStyle>) |(?i)(<Style[^>]*>(?<css>[^<]+)</Style>)"); var matches = scriptRegex.Matches(snapshot.GetText()); foreach (Match match in matches) { foreach (var groupName in new[] { "JavaScript", "TypeScript", "css" }) { var group = match.Groups[groupName]; if (group.Success) { projections.Add(new ProjectionInfo() { ContentType = groupName, Start = group.Index, End = group.Index + group.Length }); } } } return(projections); }
private void GetCompleteStatement( ITextSnapshot snapshot, SnapshotPoint snapPos, out StringBuilder sbErrors, out ITree treeStmt) { string sql = snapshot.GetText(); treeStmt = null; sbErrors = new StringBuilder(); _position = snapPos.Position; _tokens = RemoveToken(sql, snapPos); if (_tokens.Count == 1 && _tokens.Get(0).Type == MySQL51Lexer.EOF) { return; } MySQL51Parser.program_return r = LanguageServiceUtil.ParseSql(sql, false, out sbErrors, _tokens); if (r == null) { return; } ITree t = r.Tree as ITree; treeStmt = t; // locate current statement's AST if (t.IsNil) { ITree tmp = FindStmt(t); if (tmp != null) { treeStmt = tmp; } } }
public Base64Tagger(ITextBuffer buffer) { this.buffer = buffer; this.snapshot = buffer.CurrentSnapshot; this.text = snapshot.GetText(); this.buffer.Changed += BufferChanged; }
private ICollection <DeveroomTag> ParseInternal(ITextSnapshot fileSnapshot, ProjectBindingRegistry bindingRegistry, DeveroomConfiguration deveroomConfiguration) { var dialectProvider = SpecFlowGherkinDialectProvider.Get(deveroomConfiguration.DefaultFeatureLanguage); var parser = new DeveroomGherkinParser(dialectProvider, _monitoringService); parser.ParseAndCollectErrors(fileSnapshot.GetText(), _logger, out var gherkinDocument, out var parserErrors); var result = new List <DeveroomTag>(); if (gherkinDocument != null) { AddGherkinDocumentTags(fileSnapshot, bindingRegistry, gherkinDocument, result); } foreach (var parserException in parserErrors) { var line = GetSnapshotLine(parserException.Location, fileSnapshot); var startPoint = GetColumnPoint(line, parserException.Location); var span = new SnapshotSpan(startPoint, line.End); result.Add(new DeveroomTag(DeveroomTagTypes.ParserError, span, parserException.Message)); } return(result); }
internal void Parse(ITextSnapshot snapshot, out LanguageService.SyntaxTree.ITokenStream TokenStream, string path) { string source = snapshot.GetText(); // Currently we "eat" all Exception that might be raised // by XSharpSyntaxTree.ParseText TokenStream = null; try { LanguageService.CodeAnalysis.SyntaxTree tree = XSharpSyntaxTree.ParseText(source, null, path); var syntaxRoot = tree.GetRoot(); // Get the antlr4 parse tree root var xtree = ((LanguageService.CodeAnalysis.XSharp.Syntax.CompilationUnitSyntax)syntaxRoot).XSource; TokenStream = ((LanguageService.CodeAnalysis.XSharp.Syntax.CompilationUnitSyntax)syntaxRoot).XTokenStream; // var walker = new LanguageService.SyntaxTree.Tree.ParseTreeWalker(); var discover = new XSharpTreeDiscover(); discover.Snapshot = snapshot; discover.xsharpBraceCloseType = xsharpBraceCloseType; discover.xsharpBraceOpenType = xsharpBraceOpenType; discover.xsharpIdentifierType = xsharpIdentifierType; discover.xsharpRegionStartType = xsharpRegionStartType; discover.xsharpRegionStopType = xsharpRegionStopType; // Walk the tree. The TreeDiscover class will collect the tags. walker.Walk(discover, xtree); this.tags = discover.tags; } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } }
public static RapidXamlDocument Create(ITextSnapshot snapshot) { var result = new RapidXamlDocument(); try { var text = snapshot.GetText(); // TODO: review when to redo tags, etc, while invalid, or remove any tags created previously if (text.IsValidXml()) { result.RawText = text; XamlElementExtractor.Parse(snapshot, text, GetAllProcessors(), result.Tags); } } catch (Exception e) { result.Tags.Add(new UnexpectedErrorTag(new Span(0, 0), snapshot) { Description = StringRes.Error_XamlAnalysisDescription, ExtendedMessage = StringRes.Error_XamlAnalysisExtendedMessage.WithParams(e), }); RapidXamlPackage.Logger?.RecordException(e); } return(result); }
private static List <LineSpan> GetExposedLineSpans(ITextSnapshot textSnapshot) { const string start = "//["; const string end = "//]"; var bufferText = textSnapshot.GetText().ToString(); var lineSpans = new List <LineSpan>(); var lastEndIndex = 0; while (true) { var startIndex = bufferText.IndexOf(start, lastEndIndex, StringComparison.Ordinal); if (startIndex == -1) { break; } var endIndex = bufferText.IndexOf(end, lastEndIndex, StringComparison.Ordinal); var startLine = textSnapshot.GetLineNumberFromPosition(startIndex) + 1; var endLine = textSnapshot.GetLineNumberFromPosition(endIndex); lineSpans.Add(LineSpan.FromBounds(startLine, endLine)); lastEndIndex = endIndex + end.Length; } return(lineSpans); }
private static SyntaxConcept[] Parse(ITextSnapshot snapshot, out bool success) { var sb = new StringBuilder(); sb.Append("format=json tokens="); var dsl = snapshot.GetText(); sb.Append(Encoding.UTF8.GetByteCount(dsl)); var either = Compiler.CompileDsl(sb, null, dsl, cms => (ParseResult)ParseSerializer.ReadObject(cms)); if (!either.Success) { success = false; Parsed(snapshot, new ParsedArgs(either.Error)); return(new SyntaxConcept[0]); } var result = either.Value; if (result.Error != null) { var msg = (result.Error.Line >= 0 ? "Line: " + result.Error.Line + ". " : string.Empty) + result.Error.Error; Parsed(snapshot, new ParsedArgs(msg)); } else { Parsed(snapshot, new ParsedArgs()); } success = result.Error == null; if (result.Tokens == null) { return(EmptyResult); } return(result.Tokens.ToArray()); }
private IEnumerable <ITagSpan <IOutliningRegionTag> > ProcessHeadingBlocks(IEnumerable <MarkdownObject> descendants, ITextSnapshot snapshot) { var headingBlocks = descendants.OfType <HeadingBlock>(); foreach (var block in headingBlocks) { var next = headingBlocks.FirstOrDefault(h => h.Level <= block.Level && h.Line > block.Line); // Treat Setext Heading or ATX Heading uniformly var lineNumber = (next != null ? snapshot.GetLineNumberFromPosition(next.Span.Start) : snapshot.LineCount) - 1; var length = GetSectionEnding(snapshot.GetLineFromLineNumber(lineNumber)) - block.Span.Start; if (snapshot.Length >= block.Span.Start + block.Span.Length) { string text = snapshot.GetText(block.ToSimpleSpan()); var span = new SnapshotSpan(snapshot, block.Span.Start, length); var spanText = span.GetText(); if (spanText.Contains('\r') || spanText.Contains('\n')) { yield return(CreateTag(span, text, spanText)); } } } }
private GherkinFileScopeChange FullParse(ITextSnapshot textSnapshot, GherkinDialect gherkinDialect) { visualStudioTracer.Trace("Start full parsing", ParserTraceCategory); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); partialParseCount = 0; var gherkinListener = new GherkinTextBufferParserListener(gherkinDialect, textSnapshot, projectScope); var scanner = new GherkinScanner(gherkinDialect, textSnapshot.GetText(), 0); scanner.Scan(gherkinListener); var gherkinFileScope = gherkinListener.GetResult(); var result = new GherkinFileScopeChange( gherkinFileScope, true, true, gherkinFileScope.GetAllBlocks(), Enumerable.Empty <IGherkinFileBlock>()); stopwatch.Stop(); TraceFinishParse(stopwatch, "full", result); return(result); }
private GherkinFileScopeChange FullParse(ITextSnapshot textSnapshot, GherkinDialect gherkinDialect) { visualStudioTracer.Trace("Start full parsing", ParserTraceCategory); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); partialParseCount = 0; var gherkinListener = new GherkinTextBufferParserListener(gherkinDialect, textSnapshot, projectScope.Classifications); var scanner = new GherkinScanner(gherkinDialect, textSnapshot.GetText(), 0); scanner.Scan(gherkinListener); var gherkinFileScope = gherkinListener.GetResult(); var result = new GherkinFileScopeChange( gherkinFileScope, true, true, gherkinFileScope.GetAllBlocks(), Enumerable.Empty<IGherkinFileBlock>()); stopwatch.Stop(); TraceFinishParse(stopwatch, "full", result); return result; }
bool ParseAndTypeCheck() { Dafny.ModuleDecl module = new Dafny.LiteralModuleDecl(new Dafny.DefaultModuleDecl(), null); Dafny.BuiltIns builtIns = new Dafny.BuiltIns(); Dafny.Errors parseErrors = new VSErrors(this); int errorCount = Dafny.Parser.Parse(_snapshot.GetText(), _filename, module, builtIns, parseErrors); string errString = Dafny.Main.ParseIncludes(module, builtIns, new List <string>(), parseErrors); if (errorCount != 0 || errString != null) { return(false); } Dafny.Program program = new Dafny.Program(_filename, module, builtIns); var r = new VSResolver(program, this); r.ResolveProgram(program); if (r.ErrorCount != 0) { return(false); } program.AdditionalInformation.AddRange(r.AdditionalInformation); _program = program; return(true); // success }
private void ReParse() { _dirty = false; var outputWindow = OutputWindowService.TryGetPane(PredefinedOutputWindowPanes.TvlIntellisense); try { Grammar g = new Grammar(); ITextSnapshot snapshot = TextBuffer.CurrentSnapshot; g.ParseAndBuildAST(new StringReader(snapshot.GetText())); } catch (Exception e) { try { if (outputWindow != null) { outputWindow.WriteLine(e.Message); } } catch { } } }
internal void Parse(ITextSnapshot snapshot, out LanguageService.SyntaxTree.ITokenStream TokenStream, string path) { string source = snapshot.GetText(); // Currently we "eat" all Exception that might be raised // by XSharpSyntaxTree.ParseText TokenStream = null; try { LanguageService.CodeAnalysis.SyntaxTree tree = XSharpSyntaxTree.ParseText(source, null, path); var syntaxRoot = tree.GetRoot(); // Get the antlr4 parse tree root var xtree = ((LanguageService.CodeAnalysis.XSharp.Syntax.CompilationUnitSyntax)syntaxRoot).XSource; TokenStream = ((LanguageService.CodeAnalysis.XSharp.Syntax.CompilationUnitSyntax)syntaxRoot).XTokenStream; // var walker = new LanguageService.SyntaxTree.Tree.ParseTreeWalker(); var discover = new XSharpTreeDiscover(); discover.Snapshot = snapshot; discover.xsharpBraceCloseType = xsharpBraceCloseType; discover.xsharpBraceOpenType = xsharpBraceOpenType; discover.xsharpIdentifierType = xsharpIdentifierType; discover.xsharpRegionStartType = xsharpRegionStartType; discover.xsharpRegionStopType = xsharpRegionStopType; // Walk the tree. The TreeDiscover class will collect the tags. walker.Walk(discover, xtree); this.tags = discover.tags; } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } }
//===================================================================== /// <summary> /// This is called to HTML encode the current text selection /// </summary> private void HtmlEncodeSelection() { int start, end, length; string text; // Only encode a selection. Ignore the request if there isn't one. if (!textView.Selection.IsEmpty) { ITextSnapshot snapshot = textView.TextSnapshot; if (snapshot == snapshot.TextBuffer.CurrentSnapshot) { start = textView.Selection.Start.Position.Position; end = textView.Selection.End.Position.Position; length = end - start; if (length > 0) { using (var edit = snapshot.TextBuffer.CreateEdit()) { text = snapshot.GetText(start, length); // HTML encode everything but single and double quotes as they're fine as-is text = WebUtility.HtmlEncode(text).Replace(""", "\"").Replace("'", "'"); if (edit.Replace(start, length, text)) { edit.Apply(); } } } } } }
/// <summary> /// Get a local text. /// </summary> /// <returns>Text from the <see cref="TextDocument"/>.</returns> public string GetModifiedText() { ITextSnapshot textSnapshot = _textView.TextSnapshot; string text = textSnapshot.GetText(); return(text); }
public UDNParsingResults(string path, ITextSnapshot snapshot, MarkdownPackage package, Markdown markdown, FolderDetails folderDetails) { var log = new OutputPaneLogger(); ParsedSnapshot = snapshot; // Use the Publish Flag combo box to set the markdown details. markdown.PublishFlags.Clear(); // Always include public markdown.PublishFlags.Add(Settings.Default.PublicAvailabilitiesString); foreach (var flagName in package.PublishFlags) { markdown.PublishFlags.Add(flagName); } Errors = new List<ErrorDetail>(); Images = new List<ImageConversion>(); Attachments = new List<AttachmentConversionDetail>(); Document = markdown.ParseDocument(ParsedSnapshot.GetText(), Errors, Images, Attachments, folderDetails); DoxygenHelper.SetTrackedSymbols(Document.TransformationData.FoundDoxygenSymbols); CommonUnrealFunctions.CopyDocumentsImagesAndAttachments( path, log, folderDetails.AbsoluteHTMLPath, folderDetails.Language, folderDetails.CurrentFolderFromMarkdownAsTopLeaf, Images, Attachments); // Create common directories like css includes top level images etc. // Needs to be created everytime the document is generated to allow // changes to these files to show in the preview window without // restarting VS. CommonUnrealFunctions.CreateCommonDirectories( folderDetails.AbsoluteHTMLPath, folderDetails.AbsoluteMarkdownPath, log); }
public IEnumerable<HunkRangeInfo> GetGitDiffFor(ITextDocument textDocument, ITextSnapshot snapshot) { string fileName = textDocument.FilePath; GitFileStatusTracker tracker = new GitFileStatusTracker(Path.GetDirectoryName(fileName)); if (!tracker.IsGit) yield break; GitFileStatus status = tracker.GetFileStatus(fileName); if (status == GitFileStatus.New || status == GitFileStatus.Added) yield break; HistogramDiff diff = new HistogramDiff(); diff.SetFallbackAlgorithm(null); string currentText = snapshot.GetText(); byte[] preamble = textDocument.Encoding.GetPreamble(); byte[] content = textDocument.Encoding.GetBytes(currentText); if (preamble.Length > 0) { byte[] completeContent = new byte[preamble.Length + content.Length]; Buffer.BlockCopy(preamble, 0, completeContent, 0, preamble.Length); Buffer.BlockCopy(content, 0, completeContent, preamble.Length, content.Length); content = completeContent; } byte[] previousContent = null; //GetPreviousRevision(tracker, fileName); RawText b = new RawText(content); RawText a = new RawText(previousContent ?? new byte[0]); EditList edits = diff.Diff(RawTextComparator.DEFAULT, a, b); foreach (Edit edit in edits) yield return new HunkRangeInfo(snapshot, edit, a, b); }
public SnapshotCharStream(ITextSnapshot snapshot, Span cachedSpan) { //System.Diagnostics.Contracts.Contract.Requires<ArgumentNullException>(snapshot != null, "snapshot"); this.Snapshot = snapshot; this._explicitCache = true; this._currentSnapshotLineStartIndex = cachedSpan.Start; this._currentSnapshotLine = snapshot.GetText(cachedSpan); }
private LinePreservingCodeReplacer(ITextView view, string newCode, Span range) { _view = view; _snapshot = view.TextBuffer.CurrentSnapshot; _oldCode = _snapshot.GetText(range); _newCode = newCode; _newLines = newCode.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); _startingReplacementLine = _snapshot.GetLineFromPosition(range.Start).LineNumber; }
public List<Feature> ParseSnapshot(ITextSnapshot textSnapshot) { var text = textSnapshot.GetText(); var tempFile = text.ToTempFile(); var config = NBehaveConfiguration.New.SetScenarioFiles(new[] {tempFile}); var scenarioParser = new ParseScenarioFiles(config); var features = scenarioParser.LoadFiles(config.ScenarioFiles).ToList(); return features; }
public IEnumerable<ClassificationSpan> ClassifyArguments(ITextSnapshot snapshot, Span argumentsSpan, Span spanToClassify) { string arguments = snapshot.GetText(argumentsSpan); int argumentsStart = argumentsSpan.Start; foreach (var pos in GetNoConfigPositions(arguments)) { var snapshotSpan = new SnapshotSpan(snapshot, new Span(argumentsStart + pos, s_noConfigParameterNameLength)); yield return new ClassificationSpan(snapshotSpan, _registry.Keyword); } }
public override IEnumerable<ClassificationSpan> ClassifyArguments(ITextSnapshot snapshot, Span argumentsSpan, Span spanToClassify) { string arguments = snapshot.GetText(argumentsSpan); int noConfigStart, noConfigEnd; bool? init = ParseArguments(arguments, out noConfigStart, out noConfigEnd); if (noConfigStart >= 0) { yield return new ClassificationSpan(new SnapshotSpan(snapshot, Span.FromBounds(argumentsSpan.Start + noConfigStart, argumentsSpan.Start + noConfigEnd)), _registry.Keyword); } }
internal SourceText Get(ITextSnapshot textSnapshot) { Requires.NotNull(textSnapshot, nameof(textSnapshot)); SourceText sourceText = null; if (this.sources.TryGetValue(textSnapshot, out sourceText)) { return sourceText; } sourceText = new SourceText(textSnapshot.GetText()); this.sources.Add(textSnapshot, sourceText); return sourceText; }
private void ParseTextSnapshot(ITextSnapshot snapshot) { var scanner = new TemplateScanner(snapshot.GetText()); var parser = new TemplateParser(scanner); parser.Parse(); // Always return a template object, even if the parser couldn't build one, to avoid downstream errors. Template template = parser.Template ?? new Template(); var errors = new List<TemplateError>(parser.Errors); errors.AddRange(template.Validate()); this.OnTemplateChanged(new TemplateAnalysis(snapshot, template, errors)); }
private SemanticModel GetSemanticModel(ITextBuffer buffer) { if (currentSnapshot == buffer.CurrentSnapshot) return semanticModelCache; currentSnapshot = buffer.CurrentSnapshot; semanticModelCache = new SemanticModel(shadowClass); var code = currentSnapshot.GetText(); codeLexer.Tokenize(semanticModelCache, code); templateLexer.Tokenize(semanticModelCache, code); return semanticModelCache; }
public virtual string GenerateContent(ITextSnapshot snapshot, IEnumerable<ITextRange> languageBlocks, out ProjectionMapping[] mappings) { var mappingsList = new List<ProjectionMapping>(); var secondaryIndex = 0; var sb = new StringBuilder(); foreach (var b in languageBlocks) { var text = snapshot.GetText(b.Start, b.Length); secondaryIndex = sb.Length; sb.AppendLine(text); var m = new ProjectionMapping(b.Start, secondaryIndex, b.Length); mappingsList.Add(m); } mappings = mappingsList.ToArray(); return sb.ToString(); }
public IEnumerable<ClassificationSpan> ClassifyArguments(ITextSnapshot snapshot, Span argumentsSpan, Span spanToClassify) { string path; var arguments = snapshot.GetText(); int i = argumentsSpan.Start; int start, end; CommandArgumentsParser.ParsePath(arguments, ref i, out path, out start, out end); if (end > start) { yield return new ClassificationSpan(new SnapshotSpan(snapshot, Span.FromBounds(start, end)), _registry.StringLiteral); } CommandArgumentsParser.ParseTrailingTrivia(arguments, ref i, out start, out end); if (end > start) { yield return new ClassificationSpan(new SnapshotSpan(snapshot, Span.FromBounds(start, end)), _registry.Comment); } }
public static string GetText(this IArtifact artifact, ITextSnapshot snapshot) { return snapshot.GetText(artifact.InnerRange.Start, artifact.InnerRange.Length); }
internal string GetCurrentTextInSnapshot( ViewTextSpan originalSpan, ITextSnapshot textSnapshot, int? endPoint = null) { var currentSpan = GetCurrentSpanInSnapshot(originalSpan, textSnapshot); var startPosition = currentSpan.Start; var endPosition = endPoint.HasValue ? endPoint.Value : currentSpan.End; // TODO(cyrusn): What to do if the span is empty, or the end comes before the start. // Can that even happen? Not sure, so we'll just be resilient just in case. return startPosition <= endPosition ? textSnapshot.GetText(Span.FromBounds(startPosition, endPosition)) : string.Empty; }
private static bool IsSpaceCompletion(ITextSnapshot snapshot, ITrackingPoint loc) { var pos = loc.GetPosition(snapshot); if (pos > 0) { return snapshot.GetText(pos - 1, 1) == " "; } return false; }
private async Task Execute (ITextSnapshot snapshot, CancellationToken cancelToken) { this.statusbar.SetText ("Evaluating..."); int id = Interlocked.Increment (ref submissionId); string original = snapshot.GetText(); bool error = false; var result = await Instantly.Instrument (original, id); string text = result.Fold ( s => s, e => { error = true; return String.Format ("L{0}: {1}", e.Region.BeginLine, e.Message); }); if (cancelToken.IsCancellationRequested) { this.statusbar.SetText ("Evaluation canceled."); return; } if (error) { this.statusbar.SetText (text); return; } IProject project = this.dte.GetProject (this.document, text); Submission submission = null; var sink = new MemoryInstrumentationSink (() => submission.IsCanceled); submission = new Submission (id, project, sink, this.context.TestCode); submission.Tag = new Tuple<ITextSnapshot, string> (snapshot, original); this.evaluator.PushSubmission (submission); }
private static bool IsKeywordInIdentifierContext(ITextSnapshot snapshot, TokenInfoWithLine? prevToken, TokenInfoWithLine? prevPrevToken, TokenInfoWithLine token) { if (prevToken != null) { var prevValue = prevToken.Value; if (prevValue.TokenInfo.Value.Category == TokenCategory.Operator && prevValue.TokenInfo.Value.Trigger == TokenTriggers.MemberSelect) { // https://nodejstools.codeplex.com/workitem/967 // member.get return true; } if (prevValue.TokenInfo.Value.Category == TokenCategory.Keyword && snapshot.GetText(SnapshotSpanToSpan(snapshot, prevValue.TokenInfo.Value, prevValue.Line)) == "function") { // https://nodejstools.codeplex.com/workitem/976 // function static() { } return true; } if (prevPrevToken != null && prevValue.TokenInfo.Value.Category == TokenCategory.Operator) { var prevSpan = SnapshotSpanToSpan(snapshot, prevValue.TokenInfo.Value, prevValue.Line); if (snapshot.GetText(prevSpan) == "*") { var prevPrevValue = prevPrevToken.Value; var prevPrevSpan = SnapshotSpanToSpan(snapshot, prevPrevValue.TokenInfo.Value, prevPrevValue.Line); if (snapshot.GetText(prevPrevSpan) == "function") { // https://nodejstools.codeplex.com/workitem/976 // This time with a generator function... // function *static() { } return true; } } } } return false; }
private void AdornCode (ITextSnapshot snapshot, CancellationToken token = default(CancellationToken)) { if (this.context == null || this.context.LastData == null) return; AdornCode (snapshot, snapshot.GetText(), this.context.LastData, token); }
public string GetText(ITextSnapshot snapshot) { var span = GetSpan(snapshot.Version); return snapshot.GetText(span); }
private void GetCompleteStatement( ITextSnapshot snapshot, SnapshotPoint snapPos, out StringBuilder sbErrors, out ITree treeStmt) { string sql = snapshot.GetText(); treeStmt = null; position = snapPos.Position; tokens = RemoveToken(sql, snapPos); MySQL51Parser.program_return r = LanguageServiceUtil.ParseSql(sql, false, out sbErrors, tokens); if (r == null) return; ITree t = r.Tree as ITree; treeStmt = t; // locate current statement's AST if (t.IsNil) { ITree tmp = FindStmt(t); if (tmp != null) treeStmt = tmp; } }
private static byte[] GetCompleteContent(ITextDocument textDocument, ITextSnapshot snapshot) { var currentText = snapshot.GetText(); var content = textDocument.Encoding.GetBytes(currentText); var preamble = textDocument.Encoding.GetPreamble(); if (preamble.Length == 0) return content; var completeContent = new byte[preamble.Length + content.Length]; Buffer.BlockCopy(preamble, 0, completeContent, 0, preamble.Length); Buffer.BlockCopy(content, 0, completeContent, preamble.Length, content.Length); return completeContent; }
private GherkinFileEditorInfo DoScan(string fileContent, ITextSnapshot textSnapshot, int lineOffset, I18n languageService, GherkinFileEditorParserListener gherkinListener, int errorRertyCount, out ScenarioEditorInfo firstUnchangedScenario) { const int MAX_ERROR_RETRY = 5; const int NO_ERROR_RETRY_FOR_LINES = 5; firstUnchangedScenario = null; try { Lexer lexer = languageService.lexer(gherkinListener); lexer.scan(fileContent, null, 0); } catch (PartialListeningDoneException partialListeningDoneException) { firstUnchangedScenario = partialListeningDoneException.FirstUnchangedScenario; } catch(LexingError lexingError) { int? errorLine = GetErrorLine(lexingError, lineOffset); if (errorLine != null && errorLine.Value < textSnapshot.LineCount - NO_ERROR_RETRY_FOR_LINES && errorRertyCount < MAX_ERROR_RETRY) { //add error classification & continue var restartLineNumber = errorLine.Value + 1; int restartPosition = textSnapshot.GetLineFromLineNumber(restartLineNumber).Start; string restartFileContent = textSnapshot.GetText(restartPosition, textSnapshot.Length - restartPosition); gherkinListener.LineOffset = restartLineNumber; return DoScan(restartFileContent, textSnapshot, restartLineNumber, languageService, gherkinListener, errorRertyCount + 1, out firstUnchangedScenario); } } // ReSharper disable EmptyGeneralCatchClause catch // ReSharper restore EmptyGeneralCatchClause { // unknown error } return gherkinListener.GetResult(); }
public static string GetVariableName(ITextView textView, ITextSnapshot snapshot) { SnapshotPoint? pt = REditorDocument.MapCaretPositionFromView(textView); if (pt.HasValue && pt.Value > 0) { int i = pt.Value - 1; for (; i >= 0; i--) { char ch = snapshot[i]; if (!RTokenizer.IsIdentifierCharacter(ch) && ch != '$' && ch != '@') { break; } } return snapshot.GetText(Span.FromBounds(i + 1, pt.Value)); } return string.Empty; }
public static string GetText(this ITextRange range, ITextSnapshot snapshot) { return snapshot.GetText(range.ToSpan()); }
public IEnumerable<ClassificationSpan> ClassifyArguments( ITextSnapshot snapshot, Span argumentsSpan, Span spanToClassify ) { var arguments = snapshot.GetText(argumentsSpan); int timeout; if (int.TryParse(arguments, out timeout)) { yield return new ClassificationSpan( new SnapshotSpan(snapshot, argumentsSpan), _registry.NumberLiteral ); } }
public static string[] GetFunctionParameters(int position, ITextSnapshot capture, bool isAboveFunction = false) { int openFunctionLine = capture.GetLineNumberFromPosition(position - 1); if (isAboveFunction) { openFunctionLine += 1; } else { openFunctionLine -= 1; } ITextSnapshotLine line = capture.GetLineFromLineNumber(openFunctionLine); string prevLine = line.Extent.GetText(); openFunctionLine = StubUtils.GetFunctionDeclarationLineNumber(capture, openFunctionLine, isAboveFunction); //Not immediately after a function declaration if (openFunctionLine == -1) return new string[0]; prevLine = capture.GetLineFromLineNumber(openFunctionLine).GetText(); int ftnIndex = StubUtils.javaScriptFnRegex.Match(prevLine).Index; int firstParenPosition = -1; if (prevLine.IndexOf('(', ftnIndex) > -1) { firstParenPosition = capture.GetLineFromLineNumber(openFunctionLine).Start + prevLine.IndexOf('(', ftnIndex) + 1; } else { do { openFunctionLine++; prevLine = capture.GetLineFromLineNumber(openFunctionLine).GetText(); } while (!prevLine.Contains("(")); firstParenPosition = capture.GetLineFromLineNumber(openFunctionLine).Start + prevLine.IndexOf('(') + 1; } int lastParenPosition = -1; if (prevLine.IndexOf(')') > 0) { lastParenPosition = capture.GetLineFromLineNumber(openFunctionLine).Start + prevLine.IndexOf(')', prevLine.IndexOf('(')); } else { do { openFunctionLine++; prevLine = capture.GetLineFromLineNumber(openFunctionLine).GetText(); } while (!prevLine.Contains(")")); lastParenPosition = capture.GetLineFromLineNumber(openFunctionLine).Start + prevLine.IndexOf(")"); } return StubUtils .RemoveComments(capture .GetText() .Substring(firstParenPosition, (lastParenPosition - firstParenPosition))) .Split(',') .Select(param => param.Trim()) .ToArray(); }