1 module dlangide.workspace.workspacesettings;
2 
3 import dlangui.core.settings;
4 import dlangui.core.i18n;
5 import ddebug.common.debugger;
6 import dlangide.workspace.project;
7 
8 import std.array : empty;
9 import std.algorithm : equal;
10 
11 /// local settings for workspace (not supposed to put under source control)
12 class WorkspaceSettings : SettingsFile {
13 
14     this(string filename) {
15         super(filename);
16     }
17 
18     private Breakpoint[] _breakpoints;
19     private EditorBookmark[] _bookmarks;
20 
21     private string _startupProjectName;
22     @property string startupProjectName() {
23         return _startupProjectName;
24     }
25     @property void startupProjectName(string s) {
26         if (!s.equal(_startupProjectName)) {
27             _startupProjectName = s;
28             _setting["startupProject"] = s;
29             save();
30         }
31     }
32 
33     /// get all breakpoints for project (for specified source file only, if specified)
34     Breakpoint[] getProjectBreakpoints(string projectName, string projectFilePath) {
35         Breakpoint[] res;
36         for (int i = cast(int)_breakpoints.length - 1; i >= 0; i--) {
37             Breakpoint bp = _breakpoints[i];
38             if (!bp.projectName.equal(projectName))
39                 continue;
40             if (!projectFilePath.empty && !bp.projectFilePath.equal(projectFilePath))
41                 continue;
42             res ~= bp;
43         }
44         return res;
45     }
46 
47     /// get all breakpoints for project (for specified source file only, if specified)
48     void setProjectBreakpoints(string projectName, string projectFilePath, Breakpoint[] bps) {
49         bool changed = false;
50         for (int i = cast(int)_breakpoints.length - 1; i >= 0; i--) {
51             Breakpoint bp = _breakpoints[i];
52             if (!bp.projectName.equal(projectName))
53                 continue;
54             if (!projectFilePath.empty && !bp.projectFilePath.equal(projectFilePath))
55                 continue;
56             for (auto j = i; j < _breakpoints.length - 1; j++)
57                 _breakpoints[j] = _breakpoints[j + 1];
58             _breakpoints.length--;
59             changed = true;
60         }
61         if (bps.length) {
62             changed = true;
63             foreach(bp; bps)
64                 _breakpoints ~= bp;
65         }
66         if (changed) {
67             setBreakpoints(_breakpoints);
68         }
69     }
70 
71     void setBreakpoints(Breakpoint[] bps) {
72         Setting obj = _setting.settingByPath("breakpoints", SettingType.ARRAY);
73         obj.clear(SettingType.ARRAY);
74         int index = 0;
75         foreach(bp; bps) {
76             Setting bpObj = new Setting();
77             bpObj.setInteger("id", bp.id);
78             bpObj.setString("file", bp.file);
79             bpObj.setInteger("line", bp.line);
80             bpObj.setBoolean("enabled", bp.enabled);
81             bpObj.setString("projectName", bp.projectName);
82             bpObj.setString("projectFilePath", bp.projectFilePath);
83             obj[index++] = bpObj;
84         }
85         _breakpoints = bps;
86         //save();
87     }
88 
89     /// get all bookmarks for project (for specified source file only, if specified)
90     EditorBookmark[] getProjectBookmarks(string projectName, string projectFilePath) {
91         EditorBookmark[] res;
92         for (int i = cast(int)_bookmarks.length - 1; i >= 0; i--) {
93             EditorBookmark bp = _bookmarks[i];
94             if (!bp.projectName.equal(projectName))
95                 continue;
96             if (!projectFilePath.empty && !bp.projectFilePath.equal(projectFilePath))
97                 continue;
98             res ~= bp;
99         }
100         return res;
101     }
102 
103     /// get all bookmarks for project (for specified source file only, if specified)
104     void setProjectBookmarks(string projectName, string projectFilePath, EditorBookmark[] bps) {
105         bool changed = false;
106         for (int i = cast(int)_bookmarks.length - 1; i >= 0; i--) {
107             EditorBookmark bp = _bookmarks[i];
108             if (!bp.projectName.equal(projectName))
109                 continue;
110             if (!projectFilePath.empty && !bp.projectFilePath.equal(projectFilePath))
111                 continue;
112             for (auto j = i; j < _bookmarks.length - 1; j++)
113                 _bookmarks[j] = _bookmarks[j + 1];
114             _bookmarks.length--;
115             changed = true;
116         }
117         if (bps.length) {
118             changed = true;
119             foreach(bp; bps)
120                 _bookmarks ~= bp;
121         }
122         if (changed) {
123             setBookmarks(_bookmarks);
124         }
125     }
126 
127     void setBookmarks(EditorBookmark[] bps) {
128         Setting obj = _setting.settingByPath("bookmarks", SettingType.ARRAY);
129         obj.clear(SettingType.ARRAY);
130         int index = 0;
131         foreach(bp; bps) {
132             Setting bpObj = new Setting();
133             bpObj.setString("file", bp.file);
134             bpObj.setInteger("line", bp.line);
135             bpObj.setString("projectName", bp.projectName);
136             bpObj.setString("projectFilePath", bp.projectFilePath);
137             obj[index++] = bpObj;
138         }
139         _bookmarks = bps;
140         //save();
141     }
142 
143     Breakpoint[] getBreakpoints() {
144         return _breakpoints;
145     }
146 
147     EditorBookmark[] getBookmarks() {
148         return _bookmarks;
149     }
150 
151     /// override to do something after loading - e.g. set defaults
152     override void afterLoad() {
153         _startupProjectName = _setting.getString("startupProject");
154         Setting obj = _setting.settingByPath("breakpoints", SettingType.ARRAY);
155         _breakpoints = null;
156         int maxBreakpointId = 0;
157         for (int i = 0; i < obj.length; i++) {
158             Breakpoint bp = new Breakpoint();
159             Setting item = obj[i];
160             bp.id = cast(int)item.getInteger("id");
161             bp.file = item.getString("file");
162             bp.projectName = item.getString("projectName");
163             bp.projectFilePath = item.getString("projectFilePath");
164             bp.line = cast(int)item.getInteger("line");
165             bp.enabled = item.getBoolean("enabled");
166             if (bp.id > maxBreakpointId)
167                 maxBreakpointId = bp.id;
168             _breakpoints ~= bp;
169         }
170         _nextBreakpointId = maxBreakpointId + 1;
171         obj = _setting.settingByPath("bookmarks", SettingType.ARRAY);
172         _bookmarks = null;
173         for (int i = 0; i < obj.length; i++) {
174             EditorBookmark bp = new EditorBookmark();
175             Setting item = obj[i];
176             bp.file = item.getString("file");
177             bp.projectName = item.getString("projectName");
178             bp.projectFilePath = item.getString("projectFilePath");
179             bp.line = cast(int)item.getInteger("line");
180             _bookmarks ~= bp;
181         }
182     }
183 
184     override void updateDefaults() {
185     }
186 
187 }
188