Skip to content

Commit 7945787

Browse files
committed
modified addFile function
1 parent 7761b76 commit 7945787

2 files changed

Lines changed: 4 additions & 54 deletions

File tree

CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFileManager+FileManagement.swift

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ extension CEWorkspaceFileManager {
6262
func addFile(
6363
fileName: String,
6464
toFile file: CEWorkspaceFile,
65-
useExtension: String? = nil
65+
useExtension: String? = nil,
66+
contents: Data? = nil
6667
) throws -> CEWorkspaceFile {
6768
// check the folder for other files, and see what the most common file extension is
6869
do {
@@ -95,7 +96,7 @@ extension CEWorkspaceFileManager {
9596
// Create the file
9697
guard fileManager.createFile(
9798
atPath: fileUrl.path,
98-
contents: nil,
99+
contents: contents,
99100
attributes: [FileAttributeKey.creationDate: Date()]
100101
) else {
101102
throw CocoaError.error(.fileWriteUnknown, url: fileUrl)
@@ -116,57 +117,6 @@ extension CEWorkspaceFileManager {
116117
}
117118
}
118119

119-
/// This function is used to create a file with contents
120-
func addFileWithContents(
121-
fileName: String,
122-
toFile file: CEWorkspaceFile,
123-
useExtension: String? = nil,
124-
contents: Data?
125-
) throws -> CEWorkspaceFile {
126-
do {
127-
var fileExtension: String
128-
if fileName.contains(".") {
129-
fileExtension = ""
130-
} else {
131-
fileExtension = useExtension ?? findCommonFileExtension(for: file)
132-
if !fileExtension.isEmpty && !fileExtension.starts(with: ".") {
133-
fileExtension = "." + fileExtension
134-
}
135-
}
136-
137-
var fileUrl = file.nearestFolder.appendingPathComponent("\(fileName)\(fileExtension)")
138-
var fileNumber = 0
139-
while fileManager.fileExists(atPath: fileUrl.path) {
140-
fileNumber += 1
141-
fileUrl = fileUrl.deletingLastPathComponent()
142-
.appendingPathComponent("\(fileName)\(fileNumber)\(fileExtension)")
143-
}
144-
145-
guard fileUrl.fileName.isValidFilename else {
146-
throw FileManagerError.invalidFileName
147-
}
148-
149-
guard fileManager.createFile(
150-
atPath: fileUrl.path,
151-
contents: contents,
152-
attributes: [FileAttributeKey.creationDate: Date()]
153-
) else {
154-
throw CocoaError.error(.fileWriteUnknown, url: fileUrl)
155-
}
156-
157-
try rebuildFiles(fromItem: file.isFolder ? file : file.parent ?? file)
158-
notifyObservers(updatedItems: [file.isFolder ? file : file.parent ?? file])
159-
160-
guard let newFile = getFile(fileUrl.path, createIfNotFound: true) else {
161-
throw FileManagerError.fileNotIndexed
162-
}
163-
return newFile
164-
} catch {
165-
logger.error("Failed to add file with contents: \(error, privacy: .auto)")
166-
throw error
167-
}
168-
}
169-
170120
/// Finds a common file extension in the same directory as a file. Defaults to `txt` if no better alternatives
171121
/// are found.
172122
/// - Parameter file: The file to use to determine a common extension.

CodeEdit/Features/NavigatorArea/ProjectNavigator/OutlineView/ProjectNavigatorMenuActions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ extension ProjectNavigatorMenu {
123123
let clipBoardContent = NSPasteboard.general.string(forType: .string)?.data(using: .utf8)
124124
if let newFile = try workspace?
125125
.workspaceFileManager?
126-
.addFileWithContents(
126+
.addFile(
127127
fileName: "untitled",
128128
toFile: item,
129129
useExtension: "",

0 commit comments

Comments
 (0)