public ParseInformation Parse(FileName fileName, ITextSource fileContent, bool fullParseInformationRequested, IProject parentProject, CancellationToken cancellationToken) { var csharpProject = parentProject as CSharpProject; CSharpParser parser = new CSharpParser(csharpProject != null ? csharpProject.CompilerSettings : null); SyntaxTree cu = parser.Parse(fileContent, fileName); cu.Freeze(); CSharpUnresolvedFile file = cu.ToTypeSystem(); ParseInformation parseInfo; if (fullParseInformationRequested) parseInfo = new CSharpFullParseInformation(file, fileContent.Version, cu); else parseInfo = new ParseInformation(file, fileContent.Version, fullParseInformationRequested); IDocument document = fileContent as IDocument; AddCommentTags(cu, parseInfo.TagComments, fileContent, parseInfo.FileName, ref document); if (fullParseInformationRequested) { if (document == null) document = new ReadOnlyDocument(fileContent, parseInfo.FileName); ((CSharpFullParseInformation)parseInfo).newFoldings = CreateNewFoldings(cu, document); } return parseInfo; }
public void Dispose() { if (eventHandlersAreRegistered) { SD.ParserService.ParseInformationUpdated -= ParserService_ParseInformationUpdated; SD.ParserService.LoadSolutionProjectsThread.Finished -= ParserService_LoadSolutionProjectsThreadEnded; eventHandlersAreRegistered = false; } this.visitor.Dispose(); this.parseInfo = null; }
public CSharpDesignerGenerator(ICSharpDesignerLoaderContext context) { this.context = context; this.primaryParseInfo = context.GetPrimaryFileParseInformation(); this.compilation = context.GetCompilation(); // Find designer class formClass = FormsDesignerSecondaryDisplayBinding.GetDesignableClass(primaryParseInfo.UnresolvedFile, compilation, out primaryPart); initializeComponents = FormsDesignerSecondaryDisplayBinding.GetInitializeComponents(formClass); if (initializeComponents == null) throw new FormsDesignerLoadException("Could not find InitializeComponents"); }
private CSharpCompletionContext(ITextEditor editor, CSharpFullParseInformation parseInfo, ICompilation compilation, IProjectContent projectContent) { Debug.Assert(editor != null); Debug.Assert(parseInfo != null); Debug.Assert(compilation != null); Debug.Assert(projectContent != null); this.Editor = editor; this.ParseInformation = parseInfo; this.Compilation = compilation; this.ProjectContent = projectContent; this.TypeResolveContextAtCaret = parseInfo.UnresolvedFile.GetTypeResolveContext(compilation, editor.Caret.Location); this.CompletionContextProvider = new DefaultCompletionContextProvider(editor.Document, parseInfo.UnresolvedFile); }
public ParseInformation Parse(FileName fileName, ITextSource fileContent, bool fullParseInformationRequested, IProject parentProject, CancellationToken cancellationToken) { var csharpProject = parentProject as CSharpProject; CSharpParser parser = new CSharpParser(csharpProject != null ? csharpProject.CompilerSettings : null); parser.GenerateTypeSystemMode = !fullParseInformationRequested; SyntaxTree cu = parser.Parse(fileContent, fileName); cu.Freeze(); CSharpUnresolvedFile file = cu.ToTypeSystem(); ParseInformation parseInfo; if (fullParseInformationRequested) { parseInfo = new CSharpFullParseInformation(file, fileContent.Version, cu); } else { parseInfo = new ParseInformation(file, fileContent.Version, fullParseInformationRequested); } IDocument document = fileContent as IDocument; AddCommentTags(cu, parseInfo.TagComments, fileContent, parseInfo.FileName, ref document); if (fullParseInformationRequested) { if (document == null) { document = new ReadOnlyDocument(fileContent, parseInfo.FileName); } ((CSharpFullParseInformation)parseInfo).newFoldings = CreateNewFoldings(cu, document); } return(parseInfo); }
/// <summary> /// Retrieves the declaration for the specified entity. /// Returns null if the entity is not defined in C# source code. /// </summary> public static EntityDeclaration GetDeclaration(this IEntity entity, out CSharpFullParseInformation parseInfo) { if (entity == null || string.IsNullOrEmpty(entity.Region.FileName)) { parseInfo = null; return null; } parseInfo = SD.ParserService.Parse(FileName.Create(entity.Region.FileName), parentProject: entity.ParentAssembly.GetProject()) as CSharpFullParseInformation; if (parseInfo == null) return null; return parseInfo.SyntaxTree.GetNodeAt<EntityDeclaration>(entity.Region.Begin); }
string CopyFileEnd(IDocument document, CSharpFullParseInformation info) { var firstFootNode = info.SyntaxTree.Children.Reverse() .TakeWhile(node => node.NodeType == NodeType.Whitespace && (!(node is Comment) || !((Comment)node).IsDocumentation)) .LastOrDefault(); if (firstFootNode == null) return ""; int offset = document.GetOffset(firstFootNode.StartLocation); return document.GetText(offset, document.TextLength - offset); }
string CopyFileHeader(IDocument document, CSharpFullParseInformation info) { var lastHeadNode = info.SyntaxTree.Children .TakeWhile(node => node.NodeType == NodeType.Whitespace && (!(node is Comment) || !((Comment)node).IsDocumentation)) .LastOrDefault(); if (lastHeadNode == null) return ""; return document.GetText(0, document.GetOffset(lastHeadNode.EndLocation)); }
async void RunAnalysis(ITextSource textSource, CSharpFullParseInformation parseInfo) { if (markerService == null) return; if (cancellationTokenSource != null) cancellationTokenSource.Cancel(); cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = cancellationTokenSource.Token; List<InspectionTag> results = new List<InspectionTag>(); try { await Task.Run( delegate { var compilation = SD.ParserService.GetCompilationForFile(parseInfo.FileName); var resolver = parseInfo.GetResolver(compilation); var context = new SDRefactoringContext(textSource, resolver, new TextLocation(0, 0), 0, 0, cancellationToken); foreach (var issueProvider in issueProviders.Value) { if (issueProvider.CurrentSeverity == Severity.None) continue; foreach (var issue in issueProvider.GetIssues(context)) { if (issue.Start.IsEmpty || issue.End.IsEmpty) { // Issues can occur on invalid locations when analyzing incomplete code. // We'll just ignore them. continue; } results.Add(new InspectionTag( this, issueProvider, textSource.Version, issue.Description, context.GetOffset(issue.Start), context.GetOffset(issue.End), issue.IssueMarker, issue.Actions)); } } }, cancellationToken); } catch (TaskCanceledException) { } catch (OperationCanceledException) { } catch (Exception ex) { SD.Log.WarnFormatted("IssueManager crashed: {0}", ex); SD.AnalyticsMonitor.TrackException(ex); } if (!cancellationToken.IsCancellationRequested) { analyzedVersion = textSource.Version; Clear(); foreach (var newResult in results) { newResult.CreateMarker(editor.Document, markerService); } existingResults = results; } if (cancellationTokenSource != null && cancellationTokenSource.Token == cancellationToken) { // Dispose the cancellation token source if it's still the same one as we originally created cancellationTokenSource.Dispose(); cancellationTokenSource = null; } }
public void EndHighlighting() { inHighlightingGroup = false; visitor.Resolver = null; this.parseInfo = null; // TODO use this to remove cached lines which are no longer visible // var visibleDocumentLines = new HashSet<IDocumentLine>(syntaxHighlighter.GetVisibleDocumentLines()); // cachedLines.RemoveAll(c => !visibleDocumentLines.Contains(c.DocumentLine)); }
HighlightedLine DoHighlightLine(int lineNumber, IDocumentLine documentLine, CachedLine cachedLine, ITextSourceVersion newVersion) { if (parseInfo == null) { if (forceParseOnNextRefresh) { forceParseOnNextRefresh = false; parseInfo = SD.ParserService.Parse(FileName.Create(document.FileName), document) as CSharpFullParseInformation; } else { parseInfo = SD.ParserService.GetCachedParseInformation(FileName.Create(document.FileName), newVersion) as CSharpFullParseInformation; } } if (parseInfo == null) { if (invalidLines != null && !invalidLines.Contains(documentLine)) { invalidLines.Add(documentLine); //Debug.WriteLine("Semantic highlighting for line {0} - marking as invalid", lineNumber); } if (cachedLine != null) { // If there's a cached version, adjust it to the latest document changes and return it. // This avoids flickering when changing a line that contains semantic highlighting. cachedLine.Update(newVersion); #if DEBUG cachedLine.HighlightedLine.ValidateInvariants(); #endif return cachedLine.HighlightedLine; } else { return null; } } if (visitor.Resolver == null) { var compilation = SD.ParserService.GetCompilationForFile(parseInfo.FileName); visitor.Resolver = parseInfo.GetResolver(compilation); } line = new HighlightedLine(document, documentLine); this.lineNumber = lineNumber; visitor.UpdateLineInformation(lineNumber); if (Debugger.IsAttached) { parseInfo.SyntaxTree.AcceptVisitor(visitor); #if DEBUG line.ValidateInvariants(); #endif } else { try { parseInfo.SyntaxTree.AcceptVisitor(visitor); #if DEBUG line.ValidateInvariants(); #endif } catch (Exception ex) { hasCrashed = true; throw new ApplicationException("Error highlighting line " + lineNumber, ex); } } //Debug.WriteLine("Semantic highlighting for line {0} - added {1} sections", lineNumber, line.Sections.Count); if (cachedLines != null && document.Version != null) { cachedLines.Add(new CachedLine(line, document.Version)); } return line; }