2019-12-28 12:16:32 +01:00
|
|
|
local Object = require "core.object"
|
2020-05-07 22:14:46 +02:00
|
|
|
local Highlighter = require "core.doc.highlighter"
|
2020-05-08 21:29:22 +02:00
|
|
|
local syntax = require "core.syntax"
|
2019-12-28 12:16:32 +01:00
|
|
|
local config = require "core.config"
|
|
|
|
local common = require "core.common"
|
|
|
|
|
|
|
|
|
|
|
|
local Doc = Object:extend()
|
|
|
|
|
|
|
|
|
|
|
|
local function split_lines(text)
|
|
|
|
local res = {}
|
|
|
|
for line in (text .. "\n"):gmatch("(.-)\n") do
|
|
|
|
table.insert(res, line)
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
function Doc:new(filename)
|
|
|
|
self:reset()
|
|
|
|
if filename then
|
|
|
|
self:load(filename)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:reset()
|
|
|
|
self.lines = { "\n" }
|
2021-06-05 21:22:56 +02:00
|
|
|
self.selections = { 1, 1, 1, 1 }
|
2021-06-05 22:37:45 +02:00
|
|
|
self.cursor_clipboard = {}
|
2019-12-28 12:16:32 +01:00
|
|
|
self.undo_stack = { idx = 1 }
|
|
|
|
self.redo_stack = { idx = 1 }
|
|
|
|
self.clean_change_id = 1
|
2020-05-07 22:14:46 +02:00
|
|
|
self.highlighter = Highlighter(self)
|
2020-05-08 21:29:22 +02:00
|
|
|
self:reset_syntax()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:reset_syntax()
|
2020-06-08 10:44:51 +02:00
|
|
|
local header = self:get_text(1, 1, self:position_offset(1, 1, 128))
|
|
|
|
local syn = syntax.get(self.filename or "", header)
|
2020-05-08 21:29:22 +02:00
|
|
|
if self.syntax ~= syn then
|
|
|
|
self.syntax = syn
|
2020-05-09 17:23:07 +02:00
|
|
|
self.highlighter:reset()
|
2020-05-08 21:29:22 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-02-18 00:26:19 +01:00
|
|
|
function Doc:set_filename(filename)
|
|
|
|
self.filename = filename
|
2021-05-05 08:47:45 +02:00
|
|
|
self.abs_filename = system.absolute_path(filename)
|
2021-02-18 00:26:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function Doc:load(filename)
|
|
|
|
local fp = assert( io.open(filename, "rb") )
|
|
|
|
self:reset()
|
2021-02-18 00:26:19 +01:00
|
|
|
self:set_filename(filename)
|
2019-12-28 12:16:32 +01:00
|
|
|
self.lines = {}
|
|
|
|
for line in fp:lines() do
|
|
|
|
if line:byte(-1) == 13 then
|
|
|
|
line = line:sub(1, -2)
|
|
|
|
self.crlf = true
|
|
|
|
end
|
|
|
|
table.insert(self.lines, line .. "\n")
|
|
|
|
end
|
|
|
|
if #self.lines == 0 then
|
|
|
|
table.insert(self.lines, "\n")
|
|
|
|
end
|
|
|
|
fp:close()
|
2020-05-08 21:29:22 +02:00
|
|
|
self:reset_syntax()
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:save(filename)
|
|
|
|
filename = filename or assert(self.filename, "no filename set to default to")
|
|
|
|
local fp = assert( io.open(filename, "wb") )
|
|
|
|
for _, line in ipairs(self.lines) do
|
|
|
|
if self.crlf then line = line:gsub("\n", "\r\n") end
|
|
|
|
fp:write(line)
|
|
|
|
end
|
|
|
|
fp:close()
|
2021-02-18 00:26:19 +01:00
|
|
|
if filename then
|
|
|
|
self:set_filename(filename)
|
|
|
|
end
|
2020-05-08 21:29:22 +02:00
|
|
|
self:reset_syntax()
|
2019-12-28 12:16:32 +01:00
|
|
|
self:clean()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:get_name()
|
|
|
|
return self.filename or "unsaved"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:is_dirty()
|
|
|
|
return self.clean_change_id ~= self:get_change_id()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:clean()
|
|
|
|
self.clean_change_id = self:get_change_id()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:get_change_id()
|
|
|
|
return self.undo_stack.idx
|
|
|
|
end
|
|
|
|
|
2021-06-05 21:22:56 +02:00
|
|
|
function Doc:set_selections(idx, line1, col1, line2, col2, swap)
|
|
|
|
assert(not line2 == not col2, "expected 3 or 5 arguments")
|
|
|
|
if swap then line1, col1, line2, col2 = line2, col2, line1, col1 end
|
|
|
|
line1, col1 = self:sanitize_position(line1, col1)
|
2019-12-28 12:16:32 +01:00
|
|
|
line2, col2 = self:sanitize_position(line2 or line1, col2 or col1)
|
2021-06-07 02:30:09 +02:00
|
|
|
common.splice(self.selections, (idx - 1)*4 + 1, 4, { line1, col1, line2, col2 })
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2021-06-07 02:30:09 +02:00
|
|
|
function Doc:set_selection(line1, col1, line2, col2, swap)
|
|
|
|
self.selections = {}
|
|
|
|
self:set_selections(1, line1, col1, line2, col2, swap)
|
|
|
|
self.cursor_clipboard = {}
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
local function sort_positions(line1, col1, line2, col2)
|
2021-06-07 02:30:09 +02:00
|
|
|
if line1 > line2 or line1 == line2 and col1 > col2 then
|
|
|
|
return line2, col2, line1, col1
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2021-06-07 02:30:09 +02:00
|
|
|
return line1, col1, line2, col2
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Doc:get_selection(sort)
|
2021-06-07 02:30:09 +02:00
|
|
|
local idx, line1, col1, line2, col2 = self:get_selections(sort)(self.selections, 0)
|
|
|
|
return line1, col1, line2, col2
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2021-06-05 21:22:56 +02:00
|
|
|
function Doc:get_selections(sort)
|
2021-06-05 21:36:26 +02:00
|
|
|
return function(selections, idx)
|
|
|
|
local target = idx*4 + 1
|
2021-06-07 02:30:09 +02:00
|
|
|
if target >= #selections then return end
|
2021-06-05 21:22:56 +02:00
|
|
|
if sort then
|
2021-06-07 02:30:09 +02:00
|
|
|
return idx+1, sort_positions(unpack(selections, target, target+4))
|
2021-06-05 21:22:56 +02:00
|
|
|
else
|
2021-06-07 02:30:09 +02:00
|
|
|
return idx+1, unpack(selections, target, target+4)
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
2021-06-05 21:36:26 +02:00
|
|
|
end, self.selections, 0
|
2021-06-05 21:22:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-06-07 02:30:09 +02:00
|
|
|
function Doc:has_selection()
|
|
|
|
local line1, col1, line2, col2 = self:get_selection(false)
|
|
|
|
return line1 ~= line2 or col1 ~= col2
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:sanitize_selection()
|
2021-06-05 21:22:56 +02:00
|
|
|
for idx, line1, col1, line2, col2 in self:get_selections() do
|
|
|
|
self:set_selections(idx, line1, col1, line2, col2)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:sanitize_position(line, col)
|
|
|
|
line = common.clamp(line, 1, #self.lines)
|
|
|
|
col = common.clamp(col, 1, #self.lines[line])
|
|
|
|
return line, col
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function position_offset_func(self, line, col, fn, ...)
|
|
|
|
line, col = self:sanitize_position(line, col)
|
|
|
|
return fn(self, line, col, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function position_offset_byte(self, line, col, offset)
|
|
|
|
line, col = self:sanitize_position(line, col)
|
|
|
|
col = col + offset
|
|
|
|
while line > 1 and col < 1 do
|
|
|
|
line = line - 1
|
|
|
|
col = col + #self.lines[line]
|
|
|
|
end
|
|
|
|
while line < #self.lines and col > #self.lines[line] do
|
|
|
|
col = col - #self.lines[line]
|
|
|
|
line = line + 1
|
|
|
|
end
|
|
|
|
return self:sanitize_position(line, col)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function position_offset_linecol(self, line, col, lineoffset, coloffset)
|
|
|
|
return self:sanitize_position(line + lineoffset, col + coloffset)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:position_offset(line, col, ...)
|
|
|
|
if type(...) ~= "number" then
|
|
|
|
return position_offset_func(self, line, col, ...)
|
|
|
|
elseif select("#", ...) == 1 then
|
|
|
|
return position_offset_byte(self, line, col, ...)
|
|
|
|
elseif select("#", ...) == 2 then
|
|
|
|
return position_offset_linecol(self, line, col, ...)
|
|
|
|
else
|
|
|
|
error("bad number of arguments")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:get_text(line1, col1, line2, col2)
|
|
|
|
line1, col1 = self:sanitize_position(line1, col1)
|
|
|
|
line2, col2 = self:sanitize_position(line2, col2)
|
|
|
|
line1, col1, line2, col2 = sort_positions(line1, col1, line2, col2)
|
|
|
|
if line1 == line2 then
|
|
|
|
return self.lines[line1]:sub(col1, col2 - 1)
|
|
|
|
end
|
|
|
|
local lines = { self.lines[line1]:sub(col1) }
|
|
|
|
for i = line1 + 1, line2 - 1 do
|
|
|
|
table.insert(lines, self.lines[i])
|
|
|
|
end
|
|
|
|
table.insert(lines, self.lines[line2]:sub(1, col2 - 1))
|
|
|
|
return table.concat(lines)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:get_char(line, col)
|
|
|
|
line, col = self:sanitize_position(line, col)
|
|
|
|
return self.lines[line]:sub(col, col)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-14 17:10:48 +02:00
|
|
|
local function push_undo(undo_stack, time, type, ...)
|
|
|
|
undo_stack[undo_stack.idx] = { type = type, time = time, ... }
|
|
|
|
undo_stack[undo_stack.idx - config.max_undos] = nil
|
|
|
|
undo_stack.idx = undo_stack.idx + 1
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
|
2021-04-05 00:11:56 +02:00
|
|
|
local function pop_undo(self, undo_stack, redo_stack, modified)
|
2020-05-14 17:10:48 +02:00
|
|
|
-- pop command
|
|
|
|
local cmd = undo_stack[undo_stack.idx - 1]
|
|
|
|
if not cmd then return end
|
|
|
|
undo_stack.idx = undo_stack.idx - 1
|
|
|
|
|
|
|
|
-- handle command
|
|
|
|
if cmd.type == "insert" then
|
|
|
|
local line, col, text = table.unpack(cmd)
|
2021-04-06 08:18:35 +02:00
|
|
|
self:raw_insert(line, col, text, redo_stack, cmd.time)
|
2020-05-14 17:10:48 +02:00
|
|
|
elseif cmd.type == "remove" then
|
|
|
|
local line1, col1, line2, col2 = table.unpack(cmd)
|
2021-04-06 08:18:35 +02:00
|
|
|
self:raw_remove(line1, col1, line2, col2, redo_stack, cmd.time)
|
2020-05-14 17:10:48 +02:00
|
|
|
elseif cmd.type == "selection" then
|
2021-06-05 22:06:21 +02:00
|
|
|
self.selections = { unpack(cmd) }
|
2020-05-14 17:10:48 +02:00
|
|
|
end
|
|
|
|
|
2021-04-06 08:26:53 +02:00
|
|
|
modified = modified or (cmd.type ~= "selection")
|
|
|
|
|
2020-05-14 17:10:48 +02:00
|
|
|
-- if next undo command is within the merge timeout then treat as a single
|
|
|
|
-- command and continue to execute it
|
|
|
|
local next = undo_stack[undo_stack.idx - 1]
|
|
|
|
if next and math.abs(cmd.time - next.time) < config.undo_merge_timeout then
|
2021-04-06 08:26:53 +02:00
|
|
|
return pop_undo(self, undo_stack, redo_stack, modified)
|
2021-04-05 00:11:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if modified then
|
|
|
|
self:on_text_change("undo")
|
2020-05-14 17:10:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-04-06 08:18:35 +02:00
|
|
|
function Doc:raw_insert(line, col, text, undo_stack, time)
|
2019-12-28 12:16:32 +01:00
|
|
|
-- split text into lines and merge with line at insertion point
|
|
|
|
local lines = split_lines(text)
|
|
|
|
local before = self.lines[line]:sub(1, col - 1)
|
|
|
|
local after = self.lines[line]:sub(col)
|
|
|
|
for i = 1, #lines - 1 do
|
|
|
|
lines[i] = lines[i] .. "\n"
|
|
|
|
end
|
|
|
|
lines[1] = before .. lines[1]
|
|
|
|
lines[#lines] = lines[#lines] .. after
|
|
|
|
|
|
|
|
-- splice lines into line array
|
2021-06-07 02:30:09 +02:00
|
|
|
common.splice(self.lines, line, 1, lines)
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
-- push undo
|
|
|
|
local line2, col2 = self:position_offset(line, col, #text)
|
2020-05-14 17:10:48 +02:00
|
|
|
push_undo(undo_stack, time, "selection", self:get_selection())
|
|
|
|
push_undo(undo_stack, time, "remove", line, col, line2, col2)
|
2020-05-07 22:14:46 +02:00
|
|
|
|
2020-05-14 17:10:48 +02:00
|
|
|
-- update highlighter and assure selection is in bounds
|
2020-05-07 22:14:46 +02:00
|
|
|
self.highlighter:invalidate(line)
|
2020-05-14 17:10:48 +02:00
|
|
|
self:sanitize_selection()
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-04-06 08:18:35 +02:00
|
|
|
function Doc:raw_remove(line1, col1, line2, col2, undo_stack, time)
|
2019-12-28 12:16:32 +01:00
|
|
|
-- push undo
|
|
|
|
local text = self:get_text(line1, col1, line2, col2)
|
2020-05-14 17:10:48 +02:00
|
|
|
push_undo(undo_stack, time, "selection", self:get_selection())
|
|
|
|
push_undo(undo_stack, time, "insert", line1, col1, text)
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
-- get line content before/after removed text
|
|
|
|
local before = self.lines[line1]:sub(1, col1 - 1)
|
|
|
|
local after = self.lines[line2]:sub(col2)
|
|
|
|
|
|
|
|
-- splice line into line array
|
2021-06-07 02:30:09 +02:00
|
|
|
common.splice(self.lines, line1, line2 - line1 + 1, { before .. after })
|
2020-05-07 22:14:46 +02:00
|
|
|
|
2020-05-14 17:10:48 +02:00
|
|
|
-- update highlighter and assure selection is in bounds
|
2020-05-07 22:14:46 +02:00
|
|
|
self.highlighter:invalidate(line1)
|
2019-12-28 12:16:32 +01:00
|
|
|
self:sanitize_selection()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-14 17:10:48 +02:00
|
|
|
function Doc:insert(line, col, text)
|
2019-12-28 12:16:32 +01:00
|
|
|
self.redo_stack = { idx = 1 }
|
2020-05-14 17:10:48 +02:00
|
|
|
line, col = self:sanitize_position(line, col)
|
2021-04-06 08:18:35 +02:00
|
|
|
self:raw_insert(line, col, text, self.undo_stack, system.get_time())
|
|
|
|
self:on_text_change("insert")
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-14 17:10:48 +02:00
|
|
|
function Doc:remove(line1, col1, line2, col2)
|
|
|
|
self.redo_stack = { idx = 1 }
|
|
|
|
line1, col1 = self:sanitize_position(line1, col1)
|
|
|
|
line2, col2 = self:sanitize_position(line2, col2)
|
|
|
|
line1, col1, line2, col2 = sort_positions(line1, col1, line2, col2)
|
2021-04-06 08:18:35 +02:00
|
|
|
self:raw_remove(line1, col1, line2, col2, self.undo_stack, system.get_time())
|
|
|
|
self:on_text_change("remove")
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:undo()
|
2021-04-06 08:18:35 +02:00
|
|
|
pop_undo(self, self.undo_stack, self.redo_stack, false)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:redo()
|
2021-04-06 08:18:35 +02:00
|
|
|
pop_undo(self, self.redo_stack, self.undo_stack, false)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-06-05 21:22:56 +02:00
|
|
|
function Doc:text_input(text, idx)
|
2021-06-07 02:30:09 +02:00
|
|
|
for sidx, line1, col1, line2, col2 in self:get_selections() do
|
2021-06-05 21:22:56 +02:00
|
|
|
if not idx or idx == sidx then
|
2021-06-07 02:30:09 +02:00
|
|
|
if line1 ~= line2 or col1 ~= col2 then
|
2021-06-05 21:22:56 +02:00
|
|
|
self:delete_to(sidx)
|
|
|
|
end
|
2021-06-07 02:30:09 +02:00
|
|
|
self:insert(line1, col1, text)
|
2021-06-05 21:22:56 +02:00
|
|
|
self:move_to(sidx, #text)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function Doc:replace(fn)
|
2021-06-07 02:30:09 +02:00
|
|
|
local line1, col1, line2, col2 = self:get_selection(true)
|
|
|
|
if line1 == line2 and col1 == col2 then
|
2019-12-28 12:16:32 +01:00
|
|
|
line1, col1, line2, col2 = 1, 1, #self.lines, #self.lines[#self.lines]
|
|
|
|
end
|
|
|
|
local old_text = self:get_text(line1, col1, line2, col2)
|
|
|
|
local new_text, n = fn(old_text)
|
|
|
|
if old_text ~= new_text then
|
|
|
|
self:insert(line2, col2, new_text)
|
|
|
|
self:remove(line1, col1, line2, col2)
|
2021-06-07 14:25:10 +02:00
|
|
|
if line1 == line2 and col1 == col2 then
|
2019-12-28 12:16:32 +01:00
|
|
|
line2, col2 = self:position_offset(line1, col1, #new_text)
|
2021-06-07 02:30:09 +02:00
|
|
|
self:set_selection(line1, col1, line2, col2)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return n
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-06-05 21:22:56 +02:00
|
|
|
function Doc:delete_to(idx, ...)
|
|
|
|
for sidx, line1, col1, line2, col2 in self:get_selections(true) do
|
|
|
|
if not idx or sidx == idx then
|
2021-06-07 02:30:09 +02:00
|
|
|
if line1 ~= line2 or col1 ~= col2 then
|
2021-06-05 21:22:56 +02:00
|
|
|
self:remove(line1, col1, line2, col2)
|
|
|
|
else
|
2021-06-07 02:30:09 +02:00
|
|
|
local l2, c2 = self:position_offset(line1, col1, ...)
|
2021-06-05 21:22:56 +02:00
|
|
|
self:remove(line1, col1, l2, c2)
|
|
|
|
line1, col1 = sort_positions(line1, col1, l2, c2)
|
|
|
|
end
|
|
|
|
self:set_selections(sidx, line1, col1)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-06-05 21:22:56 +02:00
|
|
|
function Doc:move_to(idx, ...)
|
|
|
|
for sidx, line, col in self:get_selections() do
|
|
|
|
if not idx or sidx == idx then
|
|
|
|
self:set_selections(sidx, self:position_offset(line, col, ...))
|
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-06-05 21:22:56 +02:00
|
|
|
function Doc:select_to(idx, ...)
|
|
|
|
for sidx, line, col, line2, col2 in self:get_selections() do
|
|
|
|
if not idx or idx == sidx then
|
|
|
|
line, col = self:position_offset(line, col, ...)
|
|
|
|
self:set_selections(sidx, line, col, line2, col2)
|
|
|
|
end
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2021-06-05 06:44:35 +02:00
|
|
|
|
2021-06-05 06:50:25 +02:00
|
|
|
local function get_indent_string()
|
|
|
|
if config.tab_type == "hard" then
|
|
|
|
return "\t"
|
|
|
|
end
|
|
|
|
return string.rep(" ", config.indent_size)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- returns the size of the original indent, and the indent
|
|
|
|
-- in your config format, rounded either up or down
|
|
|
|
local function get_line_indent(line, rnd_up)
|
|
|
|
local _, e = line:find("^[ \t]+")
|
|
|
|
local soft_tab = string.rep(" ", config.indent_size)
|
|
|
|
if config.tab_type == "hard" then
|
|
|
|
local indent = e and line:sub(1, e):gsub(soft_tab, "\t") or ""
|
|
|
|
return e, indent:gsub(" +", rnd_up and "\t" or "")
|
|
|
|
else
|
|
|
|
local indent = e and line:sub(1, e):gsub("\t", soft_tab) or ""
|
|
|
|
local number = #indent / #soft_tab
|
|
|
|
return e, indent:sub(1,
|
|
|
|
(rnd_up and math.ceil(number) or math.floor(number))*#soft_tab)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-05 06:44:35 +02:00
|
|
|
-- un/indents text; behaviour varies based on selection and un/indent.
|
|
|
|
-- * if there's a selection, it will stay static around the
|
|
|
|
-- text for both indenting and unindenting.
|
|
|
|
-- * if you are in the beginning whitespace of a line, and are indenting, the
|
|
|
|
-- cursor will insert the exactly appropriate amount of spaces, and jump the
|
|
|
|
-- cursor to the beginning of first non whitespace characters
|
|
|
|
-- * if you are not in the beginning whitespace of a line, and you indent, it
|
|
|
|
-- inserts the appropriate whitespace, as if you typed them normally.
|
|
|
|
-- * if you are unindenting, the cursor will jump to the start of the line,
|
|
|
|
-- and remove the appropriate amount of spaces (or a tab).
|
2021-06-05 21:22:56 +02:00
|
|
|
function Doc:indent_text(unindent, line1, col1, line2, col2)
|
2021-06-05 06:44:35 +02:00
|
|
|
local text = get_indent_string()
|
|
|
|
local _, se = self.lines[line1]:find("^[ \t]+")
|
|
|
|
local in_beginning_whitespace = col1 == 1 or (se and col1 <= se + 1)
|
2021-06-05 06:50:25 +02:00
|
|
|
local has_selection = line1 ~= line2 or col1 ~= col2
|
|
|
|
if unindent or has_selection or in_beginning_whitespace then
|
|
|
|
local l1d, l2d = #self.lines[line1], #self.lines[line2]
|
2021-06-05 06:44:35 +02:00
|
|
|
for line = line1, line2 do
|
|
|
|
local e, rnded = get_line_indent(self.lines[line], unindent)
|
|
|
|
self:remove(line, 1, line, (e or 0) + 1)
|
|
|
|
self:insert(line, 1,
|
|
|
|
unindent and rnded:sub(1, #rnded - #text) or rnded .. text)
|
|
|
|
end
|
|
|
|
l1d, l2d = #self.lines[line1] - l1d, #self.lines[line2] - l2d
|
2021-06-06 03:18:05 +02:00
|
|
|
if (unindent or in_beginning_whitespace) and not has_selection then
|
2021-06-05 06:44:35 +02:00
|
|
|
local start_cursor = (se and se + 1 or 1) + l1d or #(self.lines[line1])
|
2021-06-05 21:22:56 +02:00
|
|
|
return line1, start_cursor, line2, start_cursor
|
2021-06-05 06:44:35 +02:00
|
|
|
end
|
2021-06-07 14:26:49 +02:00
|
|
|
return line1, col1 + l1d, line2, col2 + l2d
|
2021-06-05 06:44:35 +02:00
|
|
|
end
|
2021-06-07 14:26:49 +02:00
|
|
|
self:insert(line1, col1, text)
|
|
|
|
return line1, col1 + #text, line1, col1 + #text
|
2021-06-05 06:44:35 +02:00
|
|
|
end
|
|
|
|
|
2021-04-05 00:11:56 +02:00
|
|
|
-- For plugins to add custom actions of document change
|
|
|
|
function Doc:on_text_change(type)
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
return Doc
|