-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathFeature_Workspace.re
176 lines (147 loc) · 4.29 KB
/
Feature_Workspace.re
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* A workspace models the current 'ambient environment' of the editor, in particular:
* - Current directory
*
* ...and eventually
* - Open editors
* - Local modifications (hot exit)
* - Per-workspace configuration
*/
open Oni_Core;
open Utility;
module Log = (val Log.withNamespace("Feature_Workspace"));
[@deriving show]
type command =
| CloseFolder
| OpenFolder;
[@deriving show]
type msg =
| Command(command)
| FolderSelectionCanceled
| FolderPicked([@opaque] FpExp.t(FpExp.absolute))
| WorkingDirectoryChanged(string)
| Noop;
module Msg = {
let workingDirectoryChanged = workingDirectory =>
WorkingDirectoryChanged(workingDirectory);
};
type model = {
workingDirectory: string,
openedFolder: option(string),
rootName: string,
lastPickFromFilePicker: bool,
};
let initial = (~openedFolder: option(string), workingDirectory) => {
workingDirectory,
openedFolder,
rootName: Filename.basename(workingDirectory),
lastPickFromFilePicker: false,
};
let openedFolder = ({openedFolder, _}) => openedFolder;
let workingDirectory = ({workingDirectory, _}) => workingDirectory;
let rootName = ({rootName, _}) => rootName;
type outmsg =
| Nothing
| Effect(Isolinear.Effect.t(msg))
| WorkspaceChanged({
path: option(string),
shouldFocusExplorer: bool,
});
module Effects = {
let changeDirectory = (path: FpExp.t(FpExp.absolute)) =>
Isolinear.Effect.createWithDispatch(
~name="Feature_Workspace.changeDirectory", dispatch => {
let newDirectory = FpExp.toString(path);
switch (Luv.Path.chdir(newDirectory)) {
| Ok () => dispatch(WorkingDirectoryChanged(newDirectory))
| Error(msg) =>
Log.errorf(m =>
m("Error changing directory: %s\n", Luv.Error.strerror(msg))
)
};
});
let pickFolder =
Service_OS.Effect.Dialog.openFolder(
fun
| None => FolderSelectionCanceled
| Some(fp) => FolderPicked(fp),
);
};
let update = (msg, model) => {
switch (msg) {
| FolderSelectionCanceled => (model, Nothing)
| WorkingDirectoryChanged(workingDirectory) => (
{
workingDirectory,
rootName: Filename.basename(workingDirectory),
openedFolder: Some(workingDirectory),
// Reset lastPickFromFilePicker once we've actually changed
lastPickFromFilePicker: false,
},
WorkspaceChanged({
path: Some(workingDirectory),
// If the workspace change came from the open-folder dialog,
// focus the explorer.
shouldFocusExplorer: model.lastPickFromFilePicker,
}),
)
| Command(CloseFolder) => (
{...model, rootName: "", openedFolder: None},
WorkspaceChanged({path: None, shouldFocusExplorer: false}),
)
| Command(OpenFolder) => (model, Effect(Effects.pickFolder))
| FolderPicked(path) => (
{...model, lastPickFromFilePicker: true},
Effect(Effects.changeDirectory(path)),
)
| Noop => (model, Nothing)
};
};
// COMMANDS
module Commands = {
open Feature_Commands.Schema;
let closeFolder =
define(
~category="Workspace",
~title="Close Folder",
"workspace.action.closeFolder",
Command(CloseFolder),
);
let openFolder =
define(
~category="Workspace",
~title="Open Folder",
"_workbench.pickWorkspaceFolder",
Command(OpenFolder),
);
let openFolderArgs =
defineWithArgs(
"vscode.openFolder",
fun
| `List([uriJson, ..._]) =>
uriJson
|> Json.Decode.decode_value(Uri.decode)
|> Result.map(Uri.toFileSystemPath)
|> Result.to_option
|> OptionEx.flatMap(FpExp.absoluteCurrentPlatform)
|> Option.map(fp => FolderPicked(fp))
|> Option.value(~default=Noop)
| _ => Noop,
);
let all = model =>
model.openedFolder == None
? [openFolder, openFolderArgs]
// Always show open folder, so the user can switch folders from command palette
: [openFolder, openFolderArgs, closeFolder];
};
module MenuItems = {
open ContextMenu.Schema;
open Feature_MenuBar;
let openFolder = command(Commands.openFolder);
let closeFolder = command(Commands.closeFolder);
let group = group(~parent=Global.file, [openFolder, closeFolder]);
};
module Contributions = {
let commands = model => Commands.all(model);
let menuGroup = MenuItems.group;
};