-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy patheditorconfig.vala
More file actions
95 lines (84 loc) · 3.68 KB
/
editorconfig.vala
File metadata and controls
95 lines (84 loc) · 3.68 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
/*
* Copyright (c) 2018 elementary LLC. (https://github.com/elementary)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
public class Scratch.Plugins.EditorConfigPlugin: Peas.ExtensionBase, Scratch.Services.ActivatablePlugin {
Scratch.Services.Interface plugins;
public Object object { owned get; construct; }
private Code.FormatBar format_bar;
public void update_state () { }
public void activate () {
plugins = (Scratch.Services.Interface) object;
plugins.hook_toolbar.connect ((tb) => {
format_bar = tb.format_bar;
});
plugins.hook_document.connect ((d) => {
// Ensure use global settings by default
format_bar.tab_style_set_by_editor_config = false;
format_bar.tab_width_set_by_editor_config = false;
format_bar.set_document (d);
Scratch.Widgets.SourceView view = d.source_view;
File file = d.file;
if (file == null || !file.query_exists ()) {
return;
}
var handle = new EditorConfig.Handle ();
handle.set_conf_file_name (".editorconfig");
if (handle.parse (file.get_path ()) != 0) {
return;
}
for (int i = 0; i < handle.get_name_value_count (); i++) {
string name, val;
handle.get_name_value (i, out name, out val);
/* These are all properties (https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties) */
switch (name) {
case "indent_style":
format_bar.tab_style_set_by_editor_config = true;
var use_spaces = (val != "tab");
format_bar.set_insert_spaces_instead_of_tabs (use_spaces);
break;
case "indent_size":
case "tab_width":
format_bar.tab_width_set_by_editor_config = true;
var indent_width = (int.parse (val)).clamp (2, 16);
format_bar.set_tab_width (indent_width);
break;
case "end_of_line":
break;
case "charset":
break;
case "trim_trailing_whitespace":
break;
case "insert_final_newline":
break;
case "max_line_length":
view.right_margin_position = int.parse (val);
break;
default:
warning ("unrecognised name/value %s/%s", name, val);
break;
}
}
});
}
public void deactivate () { }
}
[ModuleInit]
public void peas_register_types (GLib.TypeModule module) {
var objmodule = module as Peas.ObjectModule;
objmodule.register_extension_type (typeof (Scratch.Services.ActivatablePlugin), typeof (Scratch.Plugins.EditorConfigPlugin));
}