forked from rgieseke/ta-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.lua
45 lines (39 loc) · 1.4 KB
/
project.lua
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
-- The __common.project__ module tries to figure out the project root directory
-- for a given file. If you have a directory structure
-- like
-- code
-- project1
-- src
-- file1.lua
-- project2
-- src
-- the function __root__ returns *project1* for `file1.lua`
-- if `code` is part of the `common.project.DIRS` table.
-- If the project root is not found in `DIRS` the file's directory is returned.
module('_m.common.project', package.seeall)
-- ## Fields
-- This `DIRS` field can be overwritten or added to in your `init.lua` after
-- the _common_ module has been loaded, for example:
-- _m.common.project.DIRS = { _USERHOME..'/modules',
-- '/home/username/code',
-- '/home/username/projects' }
-- The default value contains Textadept's directory and the user's
-- modules directory.
DIRS = { _HOME, _USERHOME..'/modules' }
-- ## Commands
-- Match a file's path with project root directories and try to return the
-- project root. If the project root is not found the file's directory
-- is returned.
function root()
local filename = buffer.filename
local project_root
if filename then
for i=1, #DIRS do
project_root = filename:match('('..DIRS[i]..'[/\\][^/\\]+)[/\\].+')
if project_root then
break
end
end
end
return project_root or buffer.filename:match('(.+)[/\\]')
end