Skip to content

Commit d7e0a68

Browse files
committed
Fix issues with cursor locations
1 parent 1130efa commit d7e0a68

3 files changed

Lines changed: 35 additions & 30 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ This plugin provides automatic commenting/uncommenting for micro.
44

55
Install with `> plugin install comment`.
66

7-
See the [docs](https://github.com/micro-editor/comment-plugin/blob/master/help/comment.md) for more details.
7+
See the [docs](https://github.com/micro-editor/comment-plugin/blob/master/help/comment-plugin.md) for more details.

comment.lua

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
VERSION = "1.0.5"
1+
VERSION = "1.0.6"
22

33
ft = {}
44

55
ft["c"] = "// %s"
6-
ft["c++"] = "// %s"
7-
ft["d"] = "// %s"
86
ft["go"] = "// %s"
7+
ft["python"] = "# %s"
8+
ft["python3"] = "# %s"
99
ft["html"] = "<!-- %s -->"
1010
ft["java"] = "// %s"
11-
ft["javascript"] = "// %s"
12-
ft["julia"] = "# %s"
13-
ft["lua"] = "-- %s"
1411
ft["perl"] = "# %s"
15-
ft["php"] = "// %s"
16-
ft["python"] = "# %s"
17-
ft["python3"] = "# %s"
18-
ft["ruby"] = "# %s"
1912
ft["rust"] = "// %s"
2013
ft["shell"] = "# %s"
14+
ft["lua"] = "-- %s"
15+
ft["javascript"] = "// %s"
16+
ft["ruby"] = "# %s"
17+
ft["d"] = "// %s"
2118
ft["swift"] = "// %s"
2219

2320
function onViewOpen(v)
@@ -34,32 +31,33 @@ function commentLine(lineN)
3431
local v = CurView()
3532
local line = v.Buf:Line(lineN)
3633
local commentType = v.Buf.Settings["commenttype"]
37-
if commentType == "" or not string.find(commentType, "%%s") then
38-
return
39-
end
40-
local commentRegex = "^%s*" .. commentType:gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%[", "%["):gsub("%%s", "(.*)")
34+
local commentRegex = "^%s*" .. commentType:gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%s", "(.*)")
35+
local sel = -v.Buf.Cursor.CurSelection
36+
local curpos = -v.Buf.Cursor.Loc
4137
local index = string.find(commentType, "%%s") - 1
4238
if string.match(line, commentRegex) then
4339
uncommentedLine = string.match(line, commentRegex)
4440
v.Buf:Replace(Loc(0, lineN), Loc(#line, lineN), GetLeadingWhitespace(line) .. uncommentedLine)
4541
if v.Buf.Cursor:HasSelection() then
46-
if v.Buf.Cursor.CurSelection[1].Y == v.Buf.Cursor.CurSelection[2].Y then
47-
v.Buf.Cursor.CurSelection[1].X = v.Buf.Cursor.CurSelection[1].X - index
48-
v.Buf.Cursor.CurSelection[2].X = v.Buf.Cursor.CurSelection[2].X - index
49-
end
42+
v.Buf.Cursor.CurSelection[1].Y = sel[1].Y
43+
v.Buf.Cursor.CurSelection[2].Y = sel[2].Y
44+
v.Buf.Cursor.CurSelection[1].X = sel[1].X
45+
v.Buf.Cursor.CurSelection[2].X = sel[2].X
5046
else
51-
v.Buf.Cursor.X = v.Buf.Cursor.X - index
47+
v.Buf.Cursor.X = curpos.X - index
48+
v.Buf.Cursor.Y = curpos.Y
5249
end
5350
else
5451
local commentedLine = commentType:gsub("%%s", trim(line))
5552
v.Buf:Replace(Loc(0, lineN), Loc(#line, lineN), GetLeadingWhitespace(line) .. commentedLine)
5653
if v.Buf.Cursor:HasSelection() then
57-
if v.Buf.Cursor.CurSelection[1].Y == v.Buf.Cursor.CurSelection[2].Y then
58-
v.Buf.Cursor.CurSelection[1].X = v.Buf.Cursor.CurSelection[1].X + index
59-
v.Buf.Cursor.CurSelection[2].X = v.Buf.Cursor.CurSelection[2].X + index
60-
end
54+
v.Buf.Cursor.CurSelection[1].Y = sel[1].Y
55+
v.Buf.Cursor.CurSelection[2].Y = sel[2].Y
56+
v.Buf.Cursor.CurSelection[1].X = sel[1].X
57+
v.Buf.Cursor.CurSelection[2].X = sel[2].X
6158
else
62-
v.Buf.Cursor.X = v.Buf.Cursor.X + index
59+
v.Buf.Cursor.X = curpos.X + index
60+
v.Buf.Cursor.Y = curpos.Y
6361
end
6462
end
6563
v.Cursor:Relocate()
@@ -76,9 +74,17 @@ function comment()
7674
local v = CurView()
7775
if v.Cursor:HasSelection() then
7876
if v.Cursor.CurSelection[1]:GreaterThan(-v.Cursor.CurSelection[2]) then
79-
commentSelection(v.Cursor.CurSelection[2].Y, v.Cursor.CurSelection[1].Y)
77+
local endLine = v.Cursor.CurSelection[1].Y
78+
if v.Cursor.CurSelection[1].X == 0 then
79+
endLine = endLine - 1
80+
end
81+
commentSelection(v.Cursor.CurSelection[2].Y, endLine)
8082
else
81-
commentSelection(v.Cursor.CurSelection[1].Y, v.Cursor.CurSelection[2].Y)
83+
local endLine = v.Cursor.CurSelection[2].Y
84+
if v.Cursor.CurSelection[2].X == 0 then
85+
endLine = endLine - 1
86+
end
87+
commentSelection(v.Cursor.CurSelection[1].Y, endLine)
8288
end
8389
else
8490
commentLine(v.Cursor.Y)
@@ -97,4 +103,3 @@ MakeCommand("comment", "comment.comment")
97103
BindKey("Alt-/", "comment.comment")
98104

99105
AddRuntimeFile("comment", "help", "help/comment-plugin.md")
100-

repo.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"Tags": ["comment", "uncomment"],
55
"Versions": [
66
{
7-
"Version": "1.0.5",
8-
"Url": "https://github.com/micro-editor/comment-plugin/archive/v1.0.5.zip",
7+
"Version": "1.0.6",
8+
"Url": "https://github.com/micro-editor/comment-plugin/archive/v1.0.6.zip",
99
"Require": {
1010
"micro": ">=1.1.0"
1111
}

0 commit comments

Comments
 (0)