public virtual void Open(string filename) { file = FileService.GetOrCreateOpenedFile(filename); files.Clear(); files.Add(file); file.CurrentView = this; this.Load(file, file.OpenRead()); textEditorControl.InitializeFormatter(); textEditorControl.ActivateQuickClassBrowserOnDemand(); file.CloseIfAllViewsClosed(); this.ToolTipText = filename; TabText = Path.GetFileName(filename); HaveFileName = true; }
/// <summary> /// Creates an OpenedFile for the new file, fills it with the file content, and saves it to disk. /// </summary> OpenedFile SaveFile(FileDescriptionTemplate newFile, string content, string binaryFileName, FileTemplateOptions options, out bool shouldOpen) { shouldOpen = false; string unresolvedFileName = StringParser.Parse(newFile.Name); // Parse twice so that tags used in included standard header are parsed string parsedContent = StringParser.Parse(StringParser.Parse(content)); if (parsedContent != null) { if (SD.EditorControlService.GlobalOptions.IndentationString != "\t") { parsedContent = parsedContent.Replace("\t", SD.EditorControlService.GlobalOptions.IndentationString); } } // when newFile.Name is "${Path}/${FileName}", there might be a useless '/' in front of the file name // if the file is created when no project is opened. So we remove single '/' or '\', but not double // '\\' (project is saved on network share). if (unresolvedFileName.StartsWith("/", StringComparison.Ordinal) && !unresolvedFileName.StartsWith("//", StringComparison.Ordinal) || unresolvedFileName.StartsWith("\\", StringComparison.Ordinal) && !unresolvedFileName.StartsWith("\\\\", StringComparison.Ordinal)) { unresolvedFileName = unresolvedFileName.Substring(1); } var project = options.Project; var fileName = FileName.Create(unresolvedFileName); if (newFile.IsDependentFile && Path.IsPathRooted(fileName)) { Directory.CreateDirectory(Path.GetDirectoryName(fileName)); if (!String.IsNullOrEmpty(binaryFileName)) { File.Copy(binaryFileName, fileName); } else { File.WriteAllText(fileName, parsedContent, SD.FileService.DefaultFileEncoding); } if (project != null) { AddTemplateFileToProject(project, newFile, fileName); } return(SD.FileService.GetOrCreateOpenedFile(fileName)); } else { if (!String.IsNullOrEmpty(binaryFileName)) { LoggingService.Warn("binary file was skipped"); return(null); } var data = SD.FileService.DefaultFileEncoding.GetBytesWithPreamble(parsedContent); OpenedFile file = null; try { if (Path.IsPathRooted(fileName)) { file = SD.FileService.GetOrCreateOpenedFile(fileName); file.SetData(data); Directory.CreateDirectory(Path.GetDirectoryName(fileName)); file.SaveToDisk(); if (project != null) { AddTemplateFileToProject(project, newFile, fileName); } } else { file = SD.FileService.CreateUntitledOpenedFile(Path.GetFileName(fileName), data); } shouldOpen = true; OpenedFile retVal = file; file = null; // don't close file when there was no exception and we're returning it return(retVal); } finally { if (file != null) { file.CloseIfAllViewsClosed(); } } } }