1#!/usr/bin/env python3
2
3# Copyright 2017 The xi-editor Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http:#www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from xi_plugin import start_plugin, Plugin, edit
18
19MATCHES = {"{": "}", "[": "]", "(": ")"}
20
21
22class BracketCloser(Plugin):
23    """Naively closes opened brackets, parens, & braces."""
24
25    def update(self, view, author, rev, start, end,
26               new_len, edit_type, text=None):
27        resp = 0
28        close_char = MATCHES.get(text)
29        if close_char:
30            # compute a delta from params:
31            new_cursor = end + new_len
32            # we set 'after_cursor' because we want the edit to appear to the right
33            # of the active cursor. we set priority=HIGH because we want this edit
34            # applied after concurrent edits.
35            resp = self.new_edit(rev, (new_cursor, new_cursor), close_char,
36                                 after_cursor=True, priority=edit.EDIT_PRIORITY_HIGH)
37        return resp
38
39
40def main():
41    start_plugin(BracketCloser())
42
43
44if __name__ == "__main__":
45    main()
46