internal IScriptObject OpenScript(string scriptPath) { solutionModified = true; // Changes should be persisted. showSaveSolutionDialog = true; if (File.Exists(scriptPath) == false) { return(null); // The file does not exist. } // If the entire script is so messed up that it cannot even be // parsed in a meaningful way, then "ParseScript" call will fail. // When that happens, we don't return "null" from here, the file // open should continue (and errors be shown as red colored text). IParsedScript parsedScript = InterfaceFactory.CreateParsedScript(); parsedScript.ParseScript(scriptPath); // Add the new script to loaded scripts. IScriptObject hostScript = new ScriptObject(parsedScript); loadedScripts.Add(hostScript); // Notify event subscribers that the script count has changed. if (null != ScriptCountChanged) { ScriptCountChangedEventArgs eventArgs = new ScriptCountChangedEventArgs(); eventArgs.ScriptsAdded.Add(hostScript); ScriptCountChanged(this, eventArgs); } // Subscribe to the new text buffer event notification. ITextBuffer textBuffer = hostScript.GetTextBuffer(); textBuffer.LinesUpdated += linesUpdatedHandler; return(hostScript); }
public bool SaveScript(bool saveAs) { if (parsedScript == null) { return(false); } string filePath = parsedScript.GetScriptPath(); if (false == saveAs) // This is just a simple save. { if (scriptState.textBuffer.ScriptModified == false) { return(true); // Nothing has changed, no need! } } if ((File.Exists(filePath) && ((File.GetAttributes(filePath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)) || SystemFile(filePath)) { if (saveAs == false) { if (!SystemFile(filePath)) { ReadOnlyDialogResult result = dialog.ShowReadOnlyDialog(true); if (result == ReadOnlyDialogResult.OverWrite) { FileAttributes attributes = File.GetAttributes(filePath); File.SetAttributes(filePath, attributes ^ FileAttributes.ReadOnly); } else if (result == ReadOnlyDialogResult.SaveAs) { saveAs = true; } else { return(false); } } else { ReadOnlyDialogResult result = dialog.ShowReadOnlyDialog(false); if (result == ReadOnlyDialogResult.SaveAs) { saveAs = true; } else { return(false); } } } } if (filePath == "" || saveAs) { string newFilePath = null; while (true) { newFilePath = PromptScriptSave(); if (string.IsNullOrEmpty(newFilePath)) { return(false); } int newFileIndex = Solution.Current.GetScriptIndexFromPath(newFilePath); if (newFileIndex != -1 && newFileIndex != Solution.Current.ActiveScriptIndex) { dialog.ShowFileAlreadyOpenDialog(); } else { break; } } // The check for the filePath is to see if the file is // being saved into the same file or a new file. if (!string.Equals(filePath, newFilePath)) { scriptState.textBuffer.ScriptModified = true; filePath = newFilePath; InitializeFileWatcher(newFilePath); } if (null != newFilePath) { TextEditorCore.Instance.Data.AddToRecentFileList(newFilePath); } } if (fileWatcher != null) { fileWatcher.EnableRaisingEvents = false; } try { StreamWriter streamWriter = new StreamWriter(filePath); string fileContent = scriptState.textBuffer.GetContent(); fileContent = fileContent.Replace("\n", "\r\n"); streamWriter.Write(fileContent); streamWriter.Close(); } catch (Exception) { IDialogProvider dialog = TextEditorCore.DialogProvider; string message = string.Format("Could not access file '{0}', please try again.", filePath); dialog.DisplayStatusMessage(StatusTypes.Warning, message, 5); return(false); } if (fileWatcher != null) { fileWatcher.EnableRaisingEvents = true; } bool parseResult = parsedScript.ParseScript(filePath); scriptState.textBuffer.ScriptModified = false; return(parseResult); }