Skip to content

Commit ecd1bca

Browse files
committed
Remove use of FileManager
1 parent d9e4a83 commit ecd1bca

5 files changed

Lines changed: 40 additions & 18 deletions

File tree

CodeEdit/Features/Tasks/Models/CEActiveTask.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class CEActiveTask: ObservableObject, Identifiable, Hashable {
4343
}.store(in: &cancellables)
4444
}
4545

46-
func run(workspaceURL: URL) {
46+
func run(workspaceURL: URL?) {
4747
self.workspaceURL = workspaceURL
4848
self.activeTaskID = UUID() // generate a new ID for this run
4949

CodeEdit/Features/Tasks/TaskManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ class TaskManager: ObservableObject {
1616

1717
@ObservedObject var workspaceSettings: CEWorkspaceSettingsData
1818

19-
private var workspaceURL: URL
19+
private var workspaceURL: URL?
2020
private var settingsListener: AnyCancellable?
2121

22-
init(workspaceSettings: CEWorkspaceSettingsData, workspaceURL: URL) {
22+
init(workspaceSettings: CEWorkspaceSettingsData, workspaceURL: URL?) {
2323
self.workspaceURL = workspaceURL
2424
self.workspaceSettings = workspaceSettings
2525

CodeEdit/Features/TerminalEmulator/Views/CEActiveTaskTerminalView.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,14 @@ class CEActiveTaskTerminalView: CELocalShellTerminalView {
6767
}
6868

6969
override func startProcess(
70-
workspaceURL url: URL,
70+
workspaceURL url: URL?,
7171
shell: Shell? = nil,
7272
environment: [String] = [],
7373
interactive: Bool = true
7474
) {
7575
// Start the shell
7676
do {
7777
let terminalSettings = Settings.shared.preferences.terminal
78-
FileManager.default.changeCurrentDirectoryPath(url.path)
7978

8079
var terminalEnvironment: [String] = Terminal.getEnvironmentVariables()
8180
terminalEnvironment.append("TERM_PROGRAM=CodeEditApp_Terminal")
@@ -92,17 +91,21 @@ class CEActiveTaskTerminalView: CELocalShellTerminalView {
9291
)
9392

9493
terminalEnvironment.append(contentsOf: environment)
94+
terminalEnvironment.append("CE_SHELL_INTEGRATION_DISABLE_PROMPT=1")
95+
terminalEnvironment.append(
96+
contentsOf: activeTask.task.environmentVariables.map({ $0.key + "=" + $0.value })
97+
)
9598

9699
process.startProcess(
97100
executable: shellPath,
98101
args: shellArgs,
99-
environment: terminalEnvironment + ["CE_SHELL_INTEGRATION_DISABLE_PROMPT=1"],
100-
execName: shell.rawValue
102+
environment: terminalEnvironment,
103+
execName: shell.rawValue,
104+
currentDirectory: URL(filePath: activeTask.task.workingDirectory, relativeTo: url).absolutePath
101105
)
102106

103107
// Feed the command and run it
104-
let data = Array(activeTask.task.command.utf8)
105-
process.send(data: data[0..<data.count])
108+
process.send(text: activeTask.task.command)
106109
process.send(data: EscapeSequences.cmdRet[0..<1])
107110
} catch {
108111
newline()
@@ -172,6 +175,13 @@ class CEActiveTaskTerminalView: CELocalShellTerminalView {
172175
return children
173176
}
174177

178+
func getBufferAsString() -> String {
179+
terminal.getText(
180+
start: .init(col: 0, row: 0),
181+
end: .init(col: terminal.cols, row: terminal.rows + terminal.buffer.yDisp)
182+
)
183+
}
184+
175185
override func dataReceived(slice: ArraySlice<UInt8>) {
176186
if enableOutput {
177187
super.dataReceived(slice: slice)

CodeEdit/Features/TerminalEmulator/Views/CELocalShellTerminalView.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import Foundation
1616
/// frame of `0`. This enables terminals to keep running in the background, and allows them to be removed and added
1717
/// back into the hierarchy for use in the utility area.
1818
///
19-
/// If there is a bug here: **there probably isn't**. Look instead in ``TerminalEmulatorView``.
19+
/// # 07/15/25
20+
/// This has now been updated so that it differs from `LocalProcessTerminalView` in enough important ways that it
21+
/// should not be removed in the future even if SwiftTerm has a change in behavior.
2022

2123
protocol CELocalShellTerminalViewDelegate: AnyObject {
2224
/// This method is invoked to notify that the terminal has been resized to the specified number of columns and rows
@@ -73,18 +75,12 @@ class CELocalShellTerminalView: CETerminalView, TerminalViewDelegate, LocalProce
7375
/// - workspaceURL: The URL of the workspace to start at.
7476
/// - shell: The shell to use, leave as `nil` to
7577
public func startProcess(
76-
workspaceURL url: URL,
78+
workspaceURL url: URL?,
7779
shell: Shell? = nil,
7880
environment: [String] = [],
7981
interactive: Bool = true
8082
) {
8183
let terminalSettings = Settings.shared.preferences.terminal
82-
// changes working directory to project root
83-
// TODO: Get rid of FileManager shared instance to prevent problems
84-
// using shared instance of FileManager might lead to problems when using
85-
// multiple workspaces. This works for now but most probably will need
86-
// to be changed later on
87-
FileManager.default.changeCurrentDirectoryPath(url.path)
8884

8985
var terminalEnvironment: [String] = Terminal.getEnvironmentVariables()
9086
terminalEnvironment.append("TERM_PROGRAM=CodeEditApp_Terminal")
@@ -114,7 +110,8 @@ class CELocalShellTerminalView: CETerminalView, TerminalViewDelegate, LocalProce
114110
executable: shellPath,
115111
args: shellArgs,
116112
environment: terminalEnvironment,
117-
execName: shell.rawValue
113+
execName: shell.rawValue,
114+
currentDirectory: url?.absolutePath
118115
)
119116
} catch {
120117
terminal.feed(text: "Failed to start a terminal session: \(error.localizedDescription)")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// LocalProcess+sendText.swift
3+
// CodeEdit
4+
//
5+
// Created by Khan Winter on 7/15/25.
6+
//
7+
8+
import SwiftTerm
9+
10+
extension LocalProcess {
11+
func send(text: String) {
12+
let array = Array(text.utf8)
13+
self.send(data: array[0..<array.count])
14+
}
15+
}

0 commit comments

Comments
 (0)