-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndenter.rb
67 lines (58 loc) · 1.54 KB
/
Indenter.rb
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
module Yoshied
class Indenter
def initialize(app)
@app = app
@tabWidth = 4
end
def tab
tabToTabstop
end
def tabToTabstop
col = @app.getColumnOfSelectionStart
spaces = " " * (@tabWidth - col % @tabWidth)
@app.replaceSelection(spaces)
end
def newline
selectTrailingWhites
insertLeadingWhites
end
def selectTrailingWhites
selstart = @app.getSelectionStart
while selstart >= 1 and whiteChar?(@app.getMainSubstr(selstart - 1, 1))
selstart -= 1
end
selend = @app.getSelectionEnd
@app.select(selstart, selend)
end
def whiteChar?(ch)
return ch == " " || ch == "\t"
end
def insertLeadingWhites
pos = @app.getSelectionStart
leadingWhites = leadingWhites(pos)
@app.replaceSelection("\n" + leadingWhites)
end
def leadingWhites(position)
res = ""
pos = @app.beginningOfLine(position)
while pos != @app.endOfDocument
ch = @app.getMainSubstr(pos, 1)
!whiteChar?(ch) and return res
res << ch
pos += 1
end
return res
end
def hungryBackspace
selection = @app.getSelection
if emptySelection?(selection) and selection.first >= 1
@app.select(selection.first - 1, selection.last)
end
selectTrailingWhites
@app.replaceSelection("")
end
def emptySelection?(selection)
return selection.first == selection.last
end
end
end