-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathutil.js
More file actions
172 lines (149 loc) · 3.75 KB
/
util.js
File metadata and controls
172 lines (149 loc) · 3.75 KB
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
import GLib from "gi://GLib";
import Gio from "gi://Gio";
import Xdp from "gi://Xdp";
export const portal = new Xdp.Portal();
export function logEnum(obj, value) {
console.log(
Object.entries(obj).find(([k, v]) => {
return v === value;
})[0]
);
}
export const settings = new Gio.Settings({
schema_id: pkg.name,
path: "/re/sonny/Workbench/",
});
export function createDataDir() {
const data_dir = GLib.build_filenamev([GLib.get_user_data_dir(), pkg.name]);
try {
Gio.File.new_for_path(data_dir).make_directory(null);
} catch (err) {
if (err.code !== Gio.IOErrorEnum.EXISTS) {
throw err;
}
}
return data_dir;
}
export function getFlatpakInfo() {
const keyFile = new GLib.KeyFile();
try {
keyFile.load_from_file("/.flatpak-info", GLib.KeyFileFlags.NONE);
} catch (err) {
if (err.code !== GLib.FileError.NOENT) {
logError(err);
}
return null;
}
return keyFile;
}
export const languages = [
{
id: "blueprint",
name: "Blueprint",
panel: "ui",
extensions: [".blp"],
types: [],
document: null,
},
{
id: "xml",
name: "GTK Builder",
panel: "ui",
extensions: [".ui"],
types: ["application/x-gtk-builder"],
document: null,
},
{
id: "javascript",
name: "JavaScript",
panel: "code",
extensions: [".js", ".mjs"],
types: ["text/javascript", "application/javascript"],
document: null,
},
{
id: "css",
name: "CSS",
panel: "style",
extensions: [".css"],
types: ["text/css"],
document: null,
},
{
id: "vala",
name: "Vala",
panel: "code",
extensions: [".vala"],
types: ["text/x-vala"],
document: null,
},
];
export function getLanguage(id) {
return languages.find((language) => language.id === id);
}
export function getLanguageForFile(file) {
let content_type;
try {
const info = file.query_info(
"standard::content-type",
Gio.FileQueryInfoFlags.NONE,
null
);
content_type = info.get_content_type();
} catch (err) {
logError(err);
}
if (!content_type) {
return;
}
const name = file.get_basename();
return languages.find(({ extensions, types }) => {
return (
types.includes(content_type) ||
extensions.some((ext) => name.endsWith(ext))
);
});
}
export function connect_signals(target, signals) {
return Object.entries(signals).map(([signal, handler]) => {
return target.connect_after(signal, handler);
});
}
export function disconnect_signals(target, handler_ids) {
handler_ids.forEach((handler_id) => target.disconnect(handler_id));
}
export function replaceBufferText(buffer, text, scroll_start = true) {
// this is against GtkSourceView not accounting an empty-string to empty-string change as user-edit
if (text === "") {
text = " ";
}
buffer.begin_user_action();
buffer.delete(buffer.get_start_iter(), buffer.get_end_iter());
buffer.insert(buffer.get_start_iter(), text, -1);
buffer.end_user_action();
scroll_start && buffer.place_cursor(buffer.get_start_iter());
}
export function decode(data) {
if (data instanceof GLib.Bytes) {
data = data.toArray();
}
return new TextDecoder().decode(data);
}
// Take a function that return a promise and returns a function
// that will discard all calls during a pending execution
// it's like a job queue with a max size of 1 and no concurrency
export function unstack(fn) {
let latest_promise;
let latest_arguments;
let pending = false;
return function unstack_wrapper(...args) {
latest_arguments = args;
if (pending) return;
if (!latest_promise) latest_promise = fn(...latest_arguments);
pending = true;
latest_promise.finally(() => {
pending = false;
latest_promise = fn(...latest_arguments);
});
};
}