static AstFormattingVisitor GetFormattingChanges(PolicyContainer policyParent, IEnumerable <string> mimeTypeChain, MonoDevelop.Ide.Gui.Document document, string input, DomRegion formattingRegion) { using (var stubData = TextEditorData.CreateImmutable(input)) { stubData.Document.FileName = document.FileName; var parser = document.HasProject ? new ICSharpCode.NRefactory.CSharp.CSharpParser(TypeSystemParser.GetCompilerArguments(document.Project)) : new ICSharpCode.NRefactory.CSharp.CSharpParser(); var compilationUnit = parser.Parse(stubData); bool hadErrors = parser.HasErrors; if (hadErrors) { using (var stubData2 = TextEditorData.CreateImmutable(input + "}")) { compilationUnit = parser.Parse(stubData2); hadErrors = parser.HasErrors; } } // try it out, if the behavior is better when working only with correct code. if (hadErrors) { return(null); } var policy = policyParent.Get <CSharpFormattingPolicy> (mimeTypeChain); var formattingVisitor = new AstFormattingVisitor(policy.CreateOptions(), stubData.Document, document.Editor.CreateNRefactoryTextEditorOptions()) { HadErrors = hadErrors, FormattingRegion = formattingRegion }; compilationUnit.AcceptVisitor(formattingVisitor); return(formattingVisitor); } }
static FormattingChanges GetFormattingChanges(PolicyContainer policyParent, IEnumerable <string> mimeTypeChain, MonoDevelop.Ide.Gui.Document document, string input, DomRegion formattingRegion, ref int formatStartOffset, ref int formatLength, bool formatLastStatementOnly) { using (var stubData = TextEditorData.CreateImmutable(input)) { stubData.Document.FileName = document.FileName; var parser = document.HasProject ? new CSharpParser(TypeSystemParser.GetCompilerArguments(document.Project)) : new CSharpParser(); var compilationUnit = parser.Parse(stubData); bool hadErrors = parser.HasErrors; if (hadErrors) { using (var stubData2 = TextEditorData.CreateImmutable(input + "}")) { compilationUnit = parser.Parse(stubData2); hadErrors = parser.HasErrors; } } // try it out, if the behavior is better when working only with correct code. if (hadErrors) { return(null); } var policy = policyParent.Get <CSharpFormattingPolicy> (mimeTypeChain); var formattingVisitor = new ICSharpCode.NRefactory.CSharp.CSharpFormatter(policy.CreateOptions(), document.Editor.CreateNRefactoryTextEditorOptions()); formattingVisitor.FormattingMode = FormattingMode.Intrusive; formattingVisitor.AddFormattingRegion(formattingRegion); var changes = formattingVisitor.AnalyzeFormatting(stubData.Document, compilationUnit); if (formatLastStatementOnly) { AstNode node = compilationUnit.GetAdjacentNodeAt <Statement> (stubData.OffsetToLocation(formatStartOffset + formatLength - 1)); if (node != null) { while (node.Role == Roles.EmbeddedStatement || node.Role == IfElseStatement.TrueRole || node.Role == IfElseStatement.FalseRole) { node = node.Parent; } // include indentation if node starts in new line var formatNode = node.GetPrevNode(); if (formatNode.Role != Roles.NewLine) { formatNode = node; } var start = stubData.LocationToOffset(formatNode.StartLocation); if (start > formatStartOffset) { var end = stubData.LocationToOffset(node.EndLocation); formatStartOffset = start; formatLength = end - start; } } } return(changes); } }
public override IEnumerable <MemberReference> FindReferences(Project project, IProjectContent content, IEnumerable <FilePath> possibleFiles, IProgressMonitor monitor, IEnumerable <object> members) { if (content == null) { throw new ArgumentNullException("content", "Project content not set."); } SetPossibleFiles(possibleFiles); SetSearchedMembers(members); var scopes = searchedMembers.Select(e => e is IEntity ? refFinder.GetSearchScopes((IEntity)e) : refFinder.GetSearchScopes((INamespace)e)); var compilation = project != null?TypeSystemService.GetCompilation(project) : content.CreateCompilation(); List <MemberReference> refs = new List <MemberReference> (); foreach (var opendoc in openDocuments) { foreach (var newRef in FindInDocument(opendoc.Item2)) { if (newRef == null || refs.Any(r => r.FileName == newRef.FileName && r.Region == newRef.Region)) { continue; } refs.Add(newRef); } } foreach (var file in files) { if (monitor != null) { monitor.Step(1); } string text = Mono.TextEditor.Utils.TextFileUtility.ReadAllText(file); if (memberName != null && text.IndexOf(memberName, StringComparison.Ordinal) < 0 && (keywordName == null || text.IndexOf(keywordName, StringComparison.Ordinal) < 0)) { continue; } using (var editor = TextEditorData.CreateImmutable(text)) { editor.Document.FileName = file; var unit = new PlayScriptParser().Parse(editor); if (unit == null) { continue; } var storedFile = content.GetFile(file); var parsedFile = storedFile as CSharpUnresolvedFile; if (parsedFile == null && storedFile is ParsedDocumentDecorator) { parsedFile = ((ParsedDocumentDecorator)storedFile).ParsedFile as CSharpUnresolvedFile; } if (parsedFile == null) { // for fallback purposes - should never happen. parsedFile = unit.ToTypeSystem(); content = content.AddOrUpdateFiles(parsedFile); compilation = content.CreateCompilation(); } foreach (var scope in scopes) { refFinder.FindReferencesInFile( scope, parsedFile, unit, compilation, (astNode, result) => { var newRef = GetReference(project, result, astNode, unit, file, editor); if (newRef == null || refs.Any(r => r.FileName == newRef.FileName && r.Region == newRef.Region)) { return; } refs.Add(newRef); }, CancellationToken.None ); } } } return(refs); }