Skip to content

Commit

Permalink
drop support for micro <2.0.4. fix bug that unicode characters are co…
Browse files Browse the repository at this point in the history
…unted wrong resulting in deleting a line.
  • Loading branch information
Lisiadito committed Jun 6, 2020
1 parent ef3b6a5 commit 50d038c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
54 changes: 30 additions & 24 deletions joinLines.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
VERSION = "1.0.3"
VERSION = "1.1.4"

function joinLines()
local v = CurView()
local micro = import("micro")
local config = import("micro/config")
local buffer = import("micro/buffer")
local util = import("micro/util")

function joinLines(v)
local a, b, c = nil, nil, v.Cursor
local selection = c:GetSelection()

Expand All @@ -11,36 +15,38 @@ function joinLines()
else
a, b = c.CurSelection[1], c.CurSelection[2]
end
a = Loc(a.X, a.Y)
b = Loc(b.X, b.Y)
a = buffer.Loc(a.X, a.Y)
b = buffer.Loc(b.X, b.Y)a
selection = c:GetSelection()
else
-- get beginning of curent line
local startLoc = Loc(0, c.Loc.Y)
local startLoc = buffer.Loc(0, c.Loc.Y)
-- get the last position of the next line
-- I use the go function because Lua string.len counts bytes which leads
-- to wrong results with some unicode characters
local xNext = utf8.RuneCountInString(v.Buf:Line(c.Loc.Y+1))
local xNext = util.CharacterCountInString(v.Buf:Line(c.Loc.Y+1))

a = startLoc
b = Loc(xNext, c.Loc.Y+1)
b = buffer.Loc(xNext, c.Loc.Y+1)
if a.x ~= b.x then
c:SetSelectionStart(startLoc)
c:SetSelectionEnd(b)
selection = c:GetSelection()
end
end

if a.x ~= b.x then
c:SetSelectionStart(startLoc)
c:SetSelectionEnd(b)
selection = c:GetSelection()
end
end
if a.x ~= b.x then
selection = util.String(selection)

if a.x ~= b.x then
-- swap all whitespaces with a single space
local modifiedSelection = string.gsub(selection, "\n%s*", " ")
-- write modified selection to buffer
v.Buf:Replace(a, b, modifiedSelection)
end
-- swap all whitespaces with a single space
local modifiedSelection = string.gsub(selection, "\n%s+", " ")
-- write modified selection to buffer
v.Buf:Replace(a, b, modifiedSelection)
end
end

MakeCommand("joinLines", "joinLines.joinLines")
BindKey("Alt-j", "joinLines.joinLines")

AddRuntimeFile("joinLines", "help", "help/join-lines-plugin.md")
function init()
config.MakeCommand("joinLines", joinLines, config.NoComplete)
config.TryBindKey("Alt-j", "lua:joinLines.joinLines", false)
config.AddRuntimeFile("joinLines", config.RTHelp, "help/join-lines-plugin.md")
end
6 changes: 3 additions & 3 deletions repo.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"Tags": ["join lines"],
"Versions": [
{
"Version": "1.0.3",
"Url": "https://github.com/Lisiadito/join-lines-plugin/archive/v1.0.3.zip",
"Version": "1.1.4",
"Url": "https://github.com/Lisiadito/join-lines-plugin/archive/v1.1.4.zip",
"Require": {
"micro": ">=1.1.0"
"micro": ">=2.0.4"
}
}
]
Expand Down

0 comments on commit 50d038c

Please sign in to comment.