public ITextBuffer Create(FileName fileName) { try { foreach (FileName name in viewContentFileNamesCollection) { if (FileUtility.IsEqualFileName(name, fileName)) { ITextBuffer buffer = WorkbenchSingleton.SafeThreadFunction(ReadFile, fileName); if (buffer != null) { return(buffer); } } } using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { if (MimeTypeDetection.FindMimeType(stream).StartsWith("text/")) { stream.Position = 0; return(new StringTextBuffer(ICSharpCode.AvalonEdit.Utils.FileReader.ReadFileContent(stream, Encoding.Default))); } } return(null); } catch (IOException) { return(null); } catch (UnauthorizedAccessException) { return(null); } }
string GetParseableFileContent(IProject project, string fileName) { // Loading the source files is done asynchronously: // While one file is parsed, the next is already loaded from disk. // Load file from memory if it is open OpenedFile file = FileService.GetOpenedFile(fileName); if (file != null) { string content; if (isOnMainThread) { content = GetFileContentFromFileDocumentProvider(file); } else { content = WorkbenchSingleton.SafeThreadFunction <OpenedFile, string>(GetFileContentFromFileDocumentProvider, file); } if (content != null) { return(content); } using (Stream s = file.OpenRead()) { Encoding encoding = defaultEncoding; return(ICSharpCode.TextEditor.Util.FileReader.ReadFileContent(s, ref encoding)); } } // load file return(ICSharpCode.TextEditor.Util.FileReader.ReadFileContent(fileName, defaultEncoding)); }
public IDocumentLine GetLine(int lineNumber) { if (WorkbenchSingleton.InvokeRequired) { return(WorkbenchSingleton.SafeThreadFunction(() => GetLine(lineNumber))); } return(new ThreadSafeDocumentLine(document.GetLine(lineNumber))); }
public ICompilationUnit Parse() { if (WorkbenchSingleton.InvokeRequired) { return(WorkbenchSingleton.SafeThreadFunction(() => Parse())); } return(ParserService.ParseViewContent(View).CompilationUnit); }
public int PositionToOffset(int line, int column) { if (WorkbenchSingleton.InvokeRequired) { return(WorkbenchSingleton.SafeThreadFunction(() => PositionToOffset(line, column))); } return(document.PositionToOffset(line, column)); }
public IViewContent GetOpenFile(string fileName) { if (WorkbenchSingleton.InvokeRequired) { return(WorkbenchSingleton.SafeThreadFunction(() => GetOpenFile(fileName))); } else { return(FileService.GetOpenFile(fileName)); } }
public string[] GetDirectories(string path) { if (WorkbenchSingleton.InvokeRequired) { return(WorkbenchSingleton.SafeThreadFunction(() => GetDirectories(path))); } else { return(Directory.GetDirectories(path)); } }
public IRefactoringDocumentView LoadRefactoringDocumentView(string fileName) { if (WorkbenchSingleton.InvokeRequired) { return(WorkbenchSingleton.SafeThreadFunction(() => LoadRefactoringDocumentView(fileName))); } else { return(new RefactoringDocumentView(fileName)); } }
/// <summary> /// Opens the document with the specified file name. /// </summary> public Document OpenDocument(string fileName) { if (WorkbenchSingleton.InvokeRequired) { return(WorkbenchSingleton.SafeThreadFunction <string, Document>(OpenDocumentInternal, fileName)); } else { return(OpenDocumentInternal(fileName)); } }
public bool CloseWorkbench(bool force) { if (WorkbenchSingleton.InvokeRequired) { return(WorkbenchSingleton.SafeThreadFunction <bool, bool>(CloseWorkbenchInternal, force)); } else { return(CloseWorkbenchInternal(force)); } }
public bool FileExists(string fileName) { if (WorkbenchSingleton.InvokeRequired) { return(WorkbenchSingleton.SafeThreadFunction(() => FileExists(fileName))); } else { return(File.Exists(fileName)); } }
static ProjectBrowserControl GetProjectBrowserControl() { if (WorkbenchSingleton.InvokeRequired) { return(WorkbenchSingleton.SafeThreadFunction(() => GetProjectBrowserControl())); } else { return(ProjectBrowserPad.Instance.ProjectBrowserControl); } }
/// <summary> /// Retrieves the file contents for the specified project items. /// </summary> public ITextBuffer Create(FileName fileName) { foreach (FileName name in viewContentFileNamesCollection) { if (FileUtility.IsEqualFileName(name, fileName)) { return(WorkbenchSingleton.SafeThreadFunction(ParserService.GetParseableFileContent, fileName.ToString())); } } try { return(new StringTextBuffer(ICSharpCode.AvalonEdit.Utils.FileReader.ReadFileContent(fileName, ParserService.DefaultFileEncoding))); } catch (IOException) { return(null); } catch (UnauthorizedAccessException) { return(null); } }
bool IsFileOpen(string fileName) { if (viewContentCollection == null) { viewContentCollection = WorkbenchSingleton.SafeThreadFunction <IViewContent[]>(GetViewContentCollection); } foreach (IViewContent content in viewContentCollection) { string contentName = content.IsUntitled ? content.UntitledName : content.FileName; if (contentName != null) { if (FileUtility.IsEqualFileName(fileName, contentName)) { return(true); } } } return(false); }
/// <summary> /// Ensures that an instance of Reflector is running and connects to it. /// </summary> /// <returns>The <see cref="IReflectorService"/> that can be used to control the running instance of Reflector.</returns> public static IntPtr FindOrStartReflector() { IntPtr hwnd = FindReflector(); if (hwnd != IntPtr.Zero) { return(hwnd); } // Get Reflector path and set it up string reflectorExeFullPath = WorkbenchSingleton.SafeThreadFunction <string>(ReflectorSetupHelper.GetReflectorExeFullPathInteractive); if (reflectorExeFullPath == null) { return(IntPtr.Zero); } // start Reflector ProcessStartInfo psi = new ProcessStartInfo(reflectorExeFullPath); psi.WorkingDirectory = Path.GetDirectoryName(psi.FileName); using (Process p = Process.Start(psi)) { try { p.WaitForInputIdle(10000); } catch (InvalidOperationException) { // can happen if Reflector is configured to run elevated } } for (int retryCount = 0; retryCount < 10; retryCount++) { hwnd = FindReflector(); if (hwnd != IntPtr.Zero) { return(hwnd); } Thread.Sleep(500); } MessageService.ShowError("${res:ReflectorAddIn.ReflectorRemotingFailed}"); return(IntPtr.Zero); }
string GetFileContent(ProjectItem item) { string fileName = item.FileName; if (IsFileOpen(fileName)) { string content; if (isOnMainThread) { content = GetFileContentFromOpenFile(fileName); } else { content = WorkbenchSingleton.SafeThreadFunction <string, string>(GetFileContentFromOpenFile, fileName); } if (content != null) { return(content); } } return(GetParseableFileContent(item.Project, fileName)); }
bool IsFileOpen(string fileName) { if (viewContentFileNamesCollection == null) { try { viewContentFileNamesCollection = WorkbenchSingleton.SafeThreadFunction <IList <string> >(FileService.GetOpenFiles); } catch (InvalidOperationException ex) { // can happen if the user closes SharpDevelop while the parser thread is running LoggingService.Warn(ex); viewContentFileNamesCollection = new string[0]; } } foreach (string contentName in viewContentFileNamesCollection) { if (contentName != null) { if (FileUtility.IsEqualFileName(fileName, contentName)) { return(true); } } } return(false); }
public R SafeThreadFunction <R>(Func <R> method) { return(WorkbenchSingleton.SafeThreadFunction <R>(method)); }
/// <summary> /// Gets the content of the specified file. /// This method is thread-safe. This method involves waiting for the main thread, so using it while /// holding a lock can lead to deadlocks. /// </summary> public static ITextBuffer GetParseableFileContent(string fileName) { return(WorkbenchSingleton.SafeThreadFunction(GetParseableFileContentInternal, fileName)); }
static void ParserUpdateStep() { object[] workbench; try { workbench = WorkbenchSingleton.SafeThreadFunction <object[]>(GetWorkbench); } catch (InvalidOperationException) { // includes ObjectDisposedException // maybe workbench has been disposed while waiting for the SafeThreadCall // can occur after workbench unload or after aborting SharpDevelop with // Application.Exit() LoggingService.Warn("InvalidOperationException while trying to invoke GetWorkbench()"); return; // abort this thread } #if ModifiedForAltaxo if (_activeModalContent != null) { IEditable editable = _activeModalContent as IEditable; if (editable != null) { string fileName = null; IParseableContent parseableContent = _activeModalContent as IParseableContent; //ivoko: Pls, do not throw text = parseableContent.ParseableText away. I NEED it. string text = null; if (parseableContent != null) { fileName = parseableContent.ParseableContentName; text = parseableContent.ParseableText; } else if (_activeModalContent is ICSharpCode.SharpDevelop.Gui.IViewContent) { fileName = ((ICSharpCode.SharpDevelop.Gui.IViewContent)_activeModalContent).FileName; } if (!(fileName == null || fileName.Length == 0)) { ParseInformation parseInformation = null; bool updated = false; if (text == null) { text = editable.Text; if (text == null) { return; } } int hash = text.GetHashCode(); if (!lastUpdateHash.ContainsKey(fileName) || lastUpdateHash[fileName] != hash) { parseInformation = ParseFile(fileName, text, true); lastUpdateHash[fileName] = hash; updated = true; } if (updated) { if (parseInformation != null && editable is IParseInformationListener) { ((IParseInformationListener)editable).ParseInformationUpdated(parseInformation); } } OnParserUpdateStepFinished(new ParserUpdateStepEventArgs(fileName, text, updated, parseInformation)); } } return; } #endif if (workbench != null) { IEditable editable = workbench[0] as IEditable; if (editable != null) { string fileName = null; IViewContent viewContent = (IViewContent)workbench[1]; IParseableContent parseableContent = workbench[0] as IParseableContent; //ivoko: Pls, do not throw text = parseableContent.ParseableText away. I NEED it. string text = null; if (parseableContent != null) { fileName = parseableContent.ParseableContentName; text = parseableContent.ParseableText; } else { fileName = viewContent.IsUntitled ? viewContent.UntitledName : viewContent.FileName; } if (!(fileName == null || fileName.Length == 0)) { ParseInformation parseInformation = null; bool updated = false; if (text == null) { text = editable.Text; if (text == null) { return; } } int hash = text.GetHashCode(); if (!lastUpdateHash.ContainsKey(fileName) || lastUpdateHash[fileName] != hash) { parseInformation = ParseFile(fileName, text, !viewContent.IsUntitled); lastUpdateHash[fileName] = hash; updated = true; } if (updated) { if (parseInformation != null && editable is IParseInformationListener) { ((IParseInformationListener)editable).ParseInformationUpdated(parseInformation); } } OnParserUpdateStepFinished(new ParserUpdateStepEventArgs(fileName, text, updated, parseInformation)); } } } }
public T Invoke <T>(Func <T> method) { return(WorkbenchSingleton.SafeThreadFunction <T>(method)); }