File: swift-indent.py

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (94 lines) | stat: -rw-r--r-- 3,080 bytes parent folder | download
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
# This file is a minimal swift-indent vim-integration.  To install:
# - Change 'binary' if swift-indent is not on the path (see below).
# - Add to your .vimrc:
#
#   map <C-I> :pyf <path-to-this-file>/swift-indent.py<cr>
#   imap <C-I> <c-o>:pyf <path-to-this-file>/swift-indent.py<cr>
#
# The first line enables swift-indent for NORMAL and VISUAL mode, the second
# line adds support for INSERT mode.  Change "C-I" to another binding if you
# need swift-indent on a different key (C-I stands for Ctrl+i).
#
# With this integration you can press the bound key and swift-indent will
# indent the current line in NORMAL and INSERT mode or the selected region in
# VISUAL mode.  The line or region is extended to the next bigger syntactic
# entity.
#
# You can also pass in the variable "l:lines" to choose the range for
# indenting.  This variable can either contain "<start line>:<end line> or
# "all" to indent the full file.  So, to indent the full file, write a function
# like:
#
# :function IndentFile()
# :  let l:lines="all"
# :  pyf <path-to-this-file>/swift-indent.py
# :endfunction
#
# It operates on the current, potentially unsaved buffer and does not create or
# save any files.  To revert a indenting, just undo.

import difflib
import platform
import subprocess
import sys

import vim

binary = 'swift-indent'
if vim.eval('exists("g:swift_indent_path")') == "1":
    binary = vim.eval('g:swift_indent_path')


def get_buffer(encoding):
    if platform.python_version_tuple()[0] == "3":
        return vim.current.buffer
    return [line.decode(encoding) for line in vim.current.buffer]


def main(argc, argv):
    encoding = vim.eval("&encoding")
    buf = get_buffer(encoding)

    if vim.eval('exists("l:lines")') == '1':
        lines = vim.eval('l:lines')
    else:
        lines = '%s:%s' % (vim.current.range.start + 1,
                           vim.current.range.end + 1)

    cursor = int(vim.eval('line2byte(line(".")) + col(".")')) - 2
    if cursor < 0:
        print("Couldn't determine cursor position.  Is your file empty?")
        return

    # avoid the cmd prompt on windows
    SI = None
    if sys.platform.startswith('win32'):
        SI = subprocess.STARTUPINFO()
        SI.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        SI.wShowWindow = subprocess.SW_HIDE

    command = [binary]
    if lines != 'all':
        command.extend(['-line-range', lines])

    p = subprocess.Popen(command,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                         stdin=subprocess.PIPE, startupinfo=SI)
    stdout, stderr = p.communicate(input='\n'.join(buf).encode(encoding))

    if stderr:
        print(stderr)

    if not stdout:
        print('No output from swift-indent (crashed?).')
        return

    lines = stdout.decode(encoding).split('\n')
    sequence = difflib.SequenceMatcher(None, buf, lines)
    for op in reversed(sequence.get_opcodes()):
        if op[0] != 'equal':
            vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]]


if __name__ == '__main__':
    main(len(sys.argv), sys.argv)