FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
flutter-tools.nvim/lua/flutter-tools/utils/path.lua at main · nvim-flutter/flutter-tools.nvim · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
nvim-flutter
/
flutter-tools.nvim
Public
Notifications
You must be signed in to change notification settings
Fork
116
Star
1.4k
Code
Issues
20
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
flutter-tools.nvim
/
lua
/
flutter-tools
/
utils
/
path.lua
Copy path
More file actions
More file actions
Latest commit
History
History
History
203 lines (174 loc) · 5.79 KB
Breadcrumbs
flutter-tools.nvim
/
lua
/
flutter-tools
/
utils
/
path.lua
Copy path
File metadata and controls
203 lines (174 loc) · 5.79 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
--
Some path utilities, copied from nvim-lsp config
local
lazy
=
require
(
"
flutter-tools.lazy
"
)
local
utils
=
lazy
.
require
(
"
flutter-tools.utils
"
)
---
@module
"
flutter-tools.utils
"
local
uv
=
vim
.
uv
local
api
=
vim
.
api
local
M
=
{}
function
M
.
exists
(
filename
)
local
stat
=
uv
.
fs_stat
(
filename
)
return
stat
and
stat
.
type
or
false
end
function
M
.
is_dir
(
filename
)
return
M
.
exists
(
filename
)
==
"
directory
"
end
function
M
.
is_file
(
filename
)
return
M
.
exists
(
filename
)
==
"
file
"
end
local
uname
=
uv
.
os_uname
()
M
.
is_mac
=
uname
.
sysname
==
"
Darwin
"
M
.
is_linux
=
uname
.
sysname
==
"
Linux
"
---
@type
boolean
M
.
is_windows
=
uname
.
version
:
match
(
"
Windows
"
)
M
.
path_sep
=
M
.
is_windows
and
"
\\
"
or
"
/
"
local
is_fs_root
if
M
.
is_windows
then
is_fs_root
=
function
(
path
)
return
path
:
match
(
"
^%a:$
"
)
end
else
is_fs_root
=
function
(
path
)
return
path
==
"
/
"
end
end
function
M
.
is_absolute
(
filename
)
if
M
.
is_windows
then
return
filename
:
match
(
"
^%a:
"
)
or
filename
:
match
(
"
^
\\\\
"
)
else
return
filename
:
match
(
"
^/
"
)
end
end
M
.
dirname
=
nil
do
local
strip_dir_pat
=
M
.
path_sep
..
"
([^
"
..
M
.
path_sep
..
"
]+)$
"
local
strip_sep_pat
=
M
.
path_sep
..
"
$
"
M
.
dirname
=
function
(
path
)
if
not
path
then
return
end
local
result
=
path
:
gsub
(
strip_sep_pat
,
"
"
):
gsub
(
strip_dir_pat
,
"
"
)
if
#
result
==
0
then
return
"
/
"
end
return
result
end
end
---
Join path segments using the os separator
---
@vararg
string
---
@return
string
function
M
.
join
(...)
local
result
=
table.concat
(
utils
.
flatten
({
...
}),
M
.
path_sep
):
gsub
(
M
.
path_sep
..
"
+
"
,
M
.
path_sep
)
return
result
end
--
Traverse the path calling cb along the way.
function
M
.
traverse_parents
(
path
,
cb
)
path
=
uv
.
fs_realpath
(
path
)
local
dir
=
path
--
Just in case our algo is buggy, don't infinite loop.
for
_
=
1
,
100
do
dir
=
M
.
dirname
(
dir
)
if
not
dir
then
return
end
--
If we can't ascend further, then stop looking.
if
cb
(
dir
,
path
)
then
return
dir
,
path
end
if
is_fs_root
(
dir
)
then
break
end
end
end
--
Iterate the path until we find the rootdir.
function
M
.
iterate_parents
(
path
)
path
=
uv
.
fs_realpath
(
path
)
local
function
it
(
_
,
v
)
if
not
v
then
return
end
if
is_fs_root
(
v
)
then
return
end
return
M
.
dirname
(
v
),
path
end
return
it
,
path
,
path
end
---
@param
root
string
?
---
@param
path
string
?
function
M
.
is_descendant
(
root
,
path
)
if
not
root
then
return
false
end
if
not
path
then
return
false
end
local
function
cb
(
dir
,
_
)
return
dir
==
root
end
local
dir
,
_
=
M
.
traverse_parents
(
path
,
cb
)
return
dir
==
root
end
function
M
.
search_ancestors
(
startpath
,
func
)
vim
.
validate
(
"
func
"
,
func
,
"
function
"
)
if
func
(
startpath
)
then
return
startpath
end
for
path
in
M
.
iterate_parents
(
startpath
)
do
if
func
(
path
)
then
return
path
end
end
end
local
function
find_nearest_root
(
patterns
,
startpath
)
local
function
matcher
(
path
)
for
_
,
pattern
in
ipairs
(
patterns
)
do
if
M
.
exists
(
vim
.
fn
.
glob
(
M
.
join
(
path
,
pattern
)))
then
return
path
end
end
end
return
M
.
search_ancestors
(
startpath
,
matcher
)
end
---
Checks for `resolution: workspace` in pubspec.yaml
---
Pattern matches VS Code Dart extension: /^resolution\s*:\s*workspace/im
---
@param
pubspec_path
string
---
@return
boolean
local
function
is_pub_workspace_member
(
pubspec_path
)
if
not
M
.
is_file
(
pubspec_path
)
then
return
false
end
local
content
=
vim
.
fn
.
readfile
(
pubspec_path
)
if
not
content
then
return
false
end
for
_
,
line
in
ipairs
(
content
)
do
if
line
:
lower
():
match
(
"
^resolution%s*:%s*workspace
"
)
then
return
true
end
end
return
false
end
---
Checks for `workspace:` field in pubspec.yaml
---
Pattern matches VS Code Dart extension: /^workspace\s*:/im
---
@param
pubspec_path
string
---
@return
boolean
local
function
is_pub_workspace_root
(
pubspec_path
)
if
not
M
.
is_file
(
pubspec_path
)
then
return
false
end
local
content
=
vim
.
fn
.
readfile
(
pubspec_path
)
if
not
content
then
return
false
end
for
_
,
line
in
ipairs
(
content
)
do
if
line
:
lower
():
match
(
"
^workspace%s*:
"
)
then
return
true
end
end
return
false
end
---
Find project root, traversing up to workspace root if in a pub workspace
---
@param
patterns
string[]
---
@param
startpath
string
---
@return
string
|
nil
function
M
.
find_root
(
patterns
,
startpath
)
local
root
=
find_nearest_root
(
patterns
,
startpath
)
if
not
root
then
return
nil
end
local
pubspec_path
=
M
.
join
(
root
,
"
pubspec.yaml
"
)
if
not
is_pub_workspace_member
(
pubspec_path
)
then
return
root
end
--
Workspace member, traverse upward to find the workspace root
local
parent
=
M
.
dirname
(
root
)
if
not
parent
or
parent
==
root
then
return
root
end
--
Check parent directly first (iterate_parents skips the starting path)
local
workspace_pubspec
=
M
.
join
(
parent
,
"
pubspec.yaml
"
)
if
is_pub_workspace_root
(
workspace_pubspec
)
then
return
parent
end
for
dir
in
M
.
iterate_parents
(
parent
)
do
workspace_pubspec
=
M
.
join
(
dir
,
"
pubspec.yaml
"
)
if
is_pub_workspace_root
(
workspace_pubspec
)
then
return
dir
end
end
return
root
end
function
M
.
current_buffer_path
()
local
current_buffer
=
api
.
nvim_get_current_buf
()
local
current_buffer_path
=
api
.
nvim_buf_get_name
(
current_buffer
)
return
current_buffer_path
end
function
M
.
get_absolute_path
(
input_path
)
--
Check if the provided path is an absolute path
if
vim
.
fn
.
isdirectory
(
input_path
)
==
1
and
not
input_path
:
match
(
"
^/
"
)
and
not
input_path
:
match
(
"
^%a:[/
\\
]
"
)
then
--
It's a relative path, so expand it to an absolute path
local
absolute_path
=
vim
.
fn
.
fnamemodify
(
input_path
,
"
:p
"
)
return
absolute_path
else
--
It's already an absolute path
return
input_path
end
end
function
M
.
is_flutter_dependency_path
(
full_path
)
local
path_parts
=
{
[[
.pub-cache
]]
,
[[
Pub\Cache
]]
,
[[
/fvm/versions/
]]
}
if
full_path
then
for
_
,
path_part
in
ipairs
(
path_parts
)
do
if
full_path
:
find
(
path_part
,
nil
,
true
)
then
return
true
end
end
end
return
false
end
return
M
Back
|
FazBrowse Home
|
New Git URL