aboutsummaryrefslogtreecommitdiff
path: root/util.lua
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2025-01-11 15:37:12 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-01-11 15:37:12 +0100
commitbae8a0a45393518bbf7914b10bf36139a977cda6 (patch)
tree6974858d868afc1d65dae8d19afc53213fe0ef99 /util.lua
parent29afc301ea05bc907444b698c91b141c26167baa (diff)
downloadvis-lspc-bae8a0a45393518bbf7914b10bf36139a977cda6.tar.gz
vis-lspc-bae8a0a45393518bbf7914b10bf36139a977cda6.zip
handle special cases in util.dirnamedetect-root
Diffstat (limited to 'util.lua')
-rw-r--r--util.lua22
1 files changed, 21 insertions, 1 deletions
diff --git a/util.lua b/util.lua
index 8d9df9b..de5ad07 100644
--- a/util.lua
+++ b/util.lua
@@ -127,7 +127,27 @@ end
-- @param the pathname
-- @return the pathname up to the last '/'
function util.dirname(name)
- return name:match('(.*[/])')
+ if name == '.' or name == '..' or name == '/' then
+ return name
+ end
+
+ -- strip a trailing path separator
+ if name:sub(#name, #name) == '/' then
+ name = name:sub(1, #name - 1)
+ end
+
+ local dirname = name:match('(.*)[/]')
+ -- There was no path separator in name.
+ if not dirname then
+ return '.'
+ end
+
+ -- The name started with the root dir.
+ if dirname == '' then
+ return '/'
+ end
+
+ return dirname
end
--- Create an iterator yielding the nth line of a file