@@ -117,36 +117,32 @@ local function find_nearest_root(patterns, startpath)
117117 return M .search_ancestors (startpath , matcher )
118118end
119119
120- --- @param pubspec_path string
121- --- @return table | nil
122- local function parse_pubspec (pubspec_path )
123- if not M .is_file (pubspec_path ) then return nil end
124- local content = vim .fn .readfile (pubspec_path )
125- if not content or # content == 0 then return nil end
126- local joined_content = table.concat (content , " \n " )
127- local ok , parsed = pcall (
128- function () return require (" flutter-tools.utils.yaml_parser" ).parse (joined_content ) end
129- )
130- if ok and parsed then return parsed end
131- return nil
132- end
133-
134120--- Checks for `resolution: workspace` in pubspec.yaml
121+ --- Pattern matches VS Code Dart extension: /^resolution\s*:\s*workspace/im
135122--- @param pubspec_path string
136123--- @return boolean
137124local function is_pub_workspace_member (pubspec_path )
138- local pubspec = parse_pubspec (pubspec_path )
139- if not pubspec then return false end
140- return pubspec .resolution == " workspace"
125+ if not M .is_file (pubspec_path ) then return false end
126+ local content = vim .fn .readfile (pubspec_path )
127+ if not content then return false end
128+ for _ , line in ipairs (content ) do
129+ if line :lower ():match (" ^resolution%s*:%s*workspace" ) then return true end
130+ end
131+ return false
141132end
142133
143134--- Checks for `workspace:` field in pubspec.yaml
135+ --- Pattern matches VS Code Dart extension: /^workspace\s*:/im
144136--- @param pubspec_path string
145137--- @return boolean
146138local function is_pub_workspace_root (pubspec_path )
147- local pubspec = parse_pubspec (pubspec_path )
148- if not pubspec then return false end
149- return pubspec .workspace ~= nil
139+ if not M .is_file (pubspec_path ) then return false end
140+ local content = vim .fn .readfile (pubspec_path )
141+ if not content then return false end
142+ for _ , line in ipairs (content ) do
143+ if line :lower ():match (" ^workspace%s*:" ) then return true end
144+ end
145+ return false
150146end
151147
152148--- Find project root, traversing up to workspace root if in a pub workspace
@@ -164,8 +160,12 @@ function M.find_root(patterns, startpath)
164160 local parent = M .dirname (root )
165161 if not parent or parent == root then return root end
166162
163+ -- Check parent directly first (iterate_parents skips the starting path)
164+ local workspace_pubspec = M .join (parent , " pubspec.yaml" )
165+ if is_pub_workspace_root (workspace_pubspec ) then return parent end
166+
167167 for dir in M .iterate_parents (parent ) do
168- local workspace_pubspec = M .join (dir , " pubspec.yaml" )
168+ workspace_pubspec = M .join (dir , " pubspec.yaml" )
169169 if is_pub_workspace_root (workspace_pubspec ) then return dir end
170170 end
171171
0 commit comments