public ITextSource GetFileContentForOpenFile(FileName fileName) { return(SD.MainThread.InvokeIfRequired( delegate { OpenedFile file = this.GetOpenedFile(fileName); if (file != null) { if (file.CurrentView != null) { IFileDocumentProvider provider = file.CurrentView.GetService <IFileDocumentProvider>(); if (provider != null) { IDocument document = provider.GetDocumentForFile(file); if (document != null) { return document.CreateSnapshot(); } } } using (Stream s = file.OpenRead()) { // load file return new StringTextSource(FileReader.ReadFileContent(s, DefaultFileEncoding)); } } return null; })); }
protected override void SaveToPrimary() { IFileDocumentProvider provider = this.PrimaryViewContent as IFileDocumentProvider; TextReader textReader = provider.GetDocumentForFile(PrimaryFile).CreateReader(); _control.Save(textReader, TextEditor); }
static ITextBuffer GetParseableFileContentInternal(string fileName) { //ITextBuffer res = project.GetParseableFileContent(fileName); //if (res != null) // return res; OpenedFile file = FileService.GetOpenedFile(fileName); if (file != null) { IFileDocumentProvider p = file.CurrentView as IFileDocumentProvider; if (p != null) { IDocument document = p.GetDocumentForFile(file); if (document != null) { return(document.CreateSnapshot()); } } using (Stream s = file.OpenRead()) { // load file return(new StringTextBuffer(ICSharpCode.AvalonEdit.Utils.FileReader.ReadFileContent(s, DefaultFileEncoding))); } } // load file return(new StringTextBuffer(ICSharpCode.AvalonEdit.Utils.FileReader.ReadFileContent(fileName, DefaultFileEncoding))); }
protected override void LoadFromPrimary() { IFileDocumentProvider provider = this.PrimaryViewContent as IFileDocumentProvider; TextReader textReader = provider.GetDocumentForFile(PrimaryFile).CreateReader(); _control.Load(textReader); _control.DataChanged += new EventHandler(PrimaryFileChanged); }
protected override void LoadFromPrimary() { IFileDocumentProvider provider = this.PrimaryViewContent as IFileDocumentProvider; IDocument document = provider.GetDocumentForFile(this.PrimaryFile); treeViewContainer.LoadXml(document.Text); XmlView view = XmlView.ForFile(this.PrimaryFile); if (view != null) { XmlView.CheckIsWellFormed(view.TextEditor); } }
static string GetFileContentFromFileDocumentProvider(OpenedFile file) { IFileDocumentProvider p = file.CurrentView as IFileDocumentProvider; if (p == null) { return(null); } IDocument document = p.GetDocumentForFile(file); if (document == null) { return(null); } return(document.TextContent); }
public static ProvidedDocumentInformation GetDocumentInformation(string fileName) { OpenedFile file = FileService.GetOpenedFile(fileName); if (file != null) { IFileDocumentProvider documentProvider = file.CurrentView as IFileDocumentProvider; if (documentProvider != null) { IDocument document = documentProvider.GetDocumentForFile(file); if (document != null) { return(new ProvidedDocumentInformation(document, fileName, 0)); } } } ITextBuffer fileContent = ParserService.GetParseableFileContent(fileName); return(new ProvidedDocumentInformation(fileContent, fileName, 0)); }
public static ProvidedDocumentInformation GetDocumentInformation(string fileName) { OpenedFile file = FileService.GetOpenedFile(fileName); if (file != null) { IFileDocumentProvider documentProvider = file.CurrentView as IFileDocumentProvider; if (documentProvider != null) { IDocument document = documentProvider.GetDocumentForFile(file); if (document != null) { return(new ProvidedDocumentInformation(document, fileName, 0)); } } } ITextBufferStrategy strategy = StringTextBufferStrategy.CreateTextBufferFromFile(fileName); return(new ProvidedDocumentInformation(strategy, fileName, 0)); }
static ITextBuffer ReadFile(FileName fileName) { OpenedFile openedFile = FileService.GetOpenedFile(fileName); if (openedFile == null) { return(null); } IFileDocumentProvider provider = FileService.GetOpenFile(fileName) as IFileDocumentProvider; if (provider == null) { return(null); } IDocument doc = provider.GetDocumentForFile(openedFile); if (doc == null) { return(null); } return(doc.CreateSnapshot()); }
/// <summary> /// Gets the content of the file using encoding auto-detection (or DefaultFileEncoding, if that fails). /// If the file is already open, gets the text in the opened view content. /// </summary> public static string GetParseableFileContent(string fileName) { IViewContent viewContent = FileService.GetOpenFile(fileName); IEditable editable = viewContent as IEditable; if (editable != null) { return(editable.Text); } //string res = project.GetParseableFileContent(fileName); //if (res != null) // return res; OpenedFile file = FileService.GetOpenedFile(fileName); if (file != null) { IFileDocumentProvider p = file.CurrentView as IFileDocumentProvider; if (p != null) { IDocument document = p.GetDocumentForFile(file); if (document != null) { return(document.TextContent); } } using (Stream s = file.OpenRead()) { // load file Encoding encoding = DefaultFileEncoding; return(ICSharpCode.TextEditor.Util.FileReader.ReadFileContent(s, ref encoding)); } } // load file return(ICSharpCode.TextEditor.Util.FileReader.ReadFileContent(fileName, DefaultFileEncoding)); }
public static void RenameReferences(List <Reference> list, string newName) { Dictionary <IDocument, FileView> modifiedDocuments = new Dictionary <IDocument, FileView>(); List <Modification> modifications = new List <Modification>(); foreach (Reference r in list) { IDocument document = null; IViewContent viewContent = null; OpenedFile file = FileService.GetOpenedFile(r.FileName); if (file != null) { viewContent = file.CurrentView; IFileDocumentProvider p = viewContent as IFileDocumentProvider; if (p != null) { document = p.GetDocumentForFile(file); } } if (document == null) { viewContent = FileService.OpenFile(r.FileName, false); IFileDocumentProvider p = viewContent as IFileDocumentProvider; if (p != null) { file = FileService.GetOpenedFile(r.FileName); System.Diagnostics.Debug.Assert(file != null, "OpenedFile not found after opening the file."); document = p.GetDocumentForFile(file); } } if (document == null) { LoggingService.Warn("RenameReferences: Could not get document for file '" + r.FileName + "'"); continue; } if (!modifiedDocuments.ContainsKey(document)) { modifiedDocuments.Add(document, new FileView() { ViewContent = viewContent, OpenedFile = file }); document.StartUndoableAction(); } ModifyDocument(modifications, document, r.Offset, r.Length, newName); } foreach (KeyValuePair <IDocument, FileView> entry in modifiedDocuments) { entry.Key.EndUndoableAction(); entry.Value.OpenedFile.MakeDirty(); if (entry.Value.ViewContent is IEditable) { ParserService.ParseViewContent(entry.Value.ViewContent); } else { ParserService.ParseFile(entry.Value.OpenedFile.FileName, entry.Key); } } }
static bool CreateEventHandlerCode(CompletionContext context, NewEventCompletionItem completionItem, out int discriminator) { ParseInformation p = ParserService.GetParseInformation(context.Editor.FileName); var unit = p.CompilationUnit; var loc = context.Editor.Document.OffsetToPosition(context.StartOffset); IClass c = unit.GetInnermostClass(loc.Line, loc.Column); discriminator = 1; if (c == null) { return(false); } IMethod initializeComponent = c.Methods[0]; CompoundClass compound = c.GetCompoundClass() as CompoundClass; IMethod invokeMethod = completionItem.EventType.ReturnType.GetMethods().FirstOrDefault(m => m.Name == "Invoke"); string handlerName = completionItem.HandlerName; if (invokeMethod == null) { throw new ArgumentException("delegateType is not a valid delegate!"); } if (compound != null) { foreach (IClass part in compound.Parts) { IMember lastMember = part.Methods.LastOrDefault(); if (lastMember != null && lastMember.ToString() == initializeComponent.ToString()) { continue; } if (completionItem.EventType.ReturnType == null) { return(false); } while (part.Methods.Any(m => m.Name == handlerName && m.Parameters.Count == invokeMethod.Parameters.Count && m.Parameters.SequenceEqual(invokeMethod.Parameters, new ParameterComparer()) )) { handlerName = completionItem.HandlerName + discriminator; discriminator++; } discriminator--; ParametrizedNode node = (ParametrizedNode)CodeGenerator.ConvertMember(invokeMethod, new ClassFinder(part, context.Editor.Caret.Line, context.Editor.Caret.Column)); node.Name = handlerName; node.Modifier = Modifiers.None; IViewContent viewContent = FileService.OpenFile(part.CompilationUnit.FileName, XamlBindingOptions.SwitchToCodeViewAfterInsertion); IFileDocumentProvider document = viewContent as IFileDocumentProvider; if (viewContent != null && document != null) { DomRegion domRegion; if (lastMember != null) { unit.ProjectContent.Language.CodeGenerator.InsertCodeAfter(lastMember, new RefactoringDocumentAdapter(document.GetDocumentForFile(viewContent.PrimaryFile)), node); domRegion = lastMember.BodyRegion; } else { unit.ProjectContent.Language.CodeGenerator.InsertCodeAtEnd(part.Region, new RefactoringDocumentAdapter(document.GetDocumentForFile(viewContent.PrimaryFile)), node); domRegion = part.Region; } // move caret to generated code ITextEditorProvider provider = viewContent as ITextEditorProvider; if (provider != null) { provider.TextEditor.JumpTo(domRegion.EndLine + 2, domRegion.EndColumn - 1); } } return(true); } } return(false); }