Skip to content

Commit

Permalink
feat(config): add possibility to provide cwd via project configuration (
Browse files Browse the repository at this point in the history
#383)

Fixes #329
  • Loading branch information
sidlatau committed Aug 29, 2024
1 parent 3d12583 commit 7efc0d8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,18 @@ require("flutter-tools").setup {
enabled = false,
-- if empty dap will not stop on any exceptions, otherwise it will stop on those specified
-- see |:help dap.set_exception_breakpoints()| for more info
exception_breakpoints = {}
exception_breakpoints = {},
-- Whether to call toString() on objects in debug views like hovers and the
-- variables list.
-- Invoking toString() has a performance cost and may introduce side-effects,
-- although users may expected this functionality. null is treated like false.
evaluate_to_string_in_debug_views = true,
register_configurations = function(paths)
require("dap").configurations.dart = {
<put here config that you would find in .vscode/launch.json>
--put here config that you would find in .vscode/launch.json
}
-- If you want to load .vscode launch.json automatically run the following:
-- require("dap.ext.vscode").load_launchjs()
end,
},
flutter_path = "<full/path/if/needed>", -- <-- this takes priority over the lookup
Expand Down Expand Up @@ -271,7 +273,7 @@ require("flutter-tools").setup {
virtual_text_str = "", -- the virtual text character to highlight
},
on_attach = my_custom_on_attach,
capabilities = my_custom_capabilities -- e.g. lsp_status capabilities
capabilities = my_custom_capabilities, -- e.g. lsp_status capabilities
--- OR you can specify a function to deactivate or change or control how the config is created
capabilities = function(config)
config.specificThingIDontWant = false
Expand Down Expand Up @@ -319,6 +321,7 @@ require('flutter-tools').setup_project({
name = 'Development', -- an arbitrary name that you provide so you can recognise this config
flavor = 'DevFlavor', -- your flavour
target = 'lib/main_dev.dart', -- your target
cwd = 'example', -- the working directory for the project. Optional, defaults to the LSP root directory.
device = 'pixel6pro', -- the device ID, which you can get by running `flutter devices`
dart_define = {
API_URL = 'https://dev.example.com/api',
Expand Down
15 changes: 9 additions & 6 deletions doc/flutter-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,18 @@ both are set.
enabled = false,
-- if empty dap will not stop on any exceptions, otherwise it will stop on those specified
-- see |:help dap.set_exception_breakpoints()| for more info
exception_breakpoints = {}
exception_breakpoints = {},
-- Whether to call toString() on objects in debug views like hovers and the
-- variables list.
-- Invoking toString() has a performance cost and may introduce side-effects,
-- although users may expected this functionality. null is treated like false.
evaluate_to_string_in_debug_views = true,
register_configurations = function(paths)
require("dap").configurations.dart = {
<put here config that you would find in .vscode/launch.json>
}
require("dap").configurations.dart = {
--put here config that you would find in .vscode/launch.json
}
-- If you want to load .vscode launch.json automatically run the following:
-- require("dap.ext.vscode").load_launchjs()
end,
},
flutter_path = "<full/path/if/needed>", -- <-- this takes priority over the lookup
Expand Down Expand Up @@ -307,7 +309,7 @@ both are set.
virtual_text_str = "■", -- the virtual text character to highlight
},
on_attach = my_custom_on_attach,
capabilities = my_custom_capabilities -- e.g. lsp_status capabilities
capabilities = my_custom_capabilities, -- e.g. lsp_status capabilities
--- OR you can specify a function to deactivate or change or control how the config is created
capabilities = function(config)
config.specificThingIDontWant = false
Expand Down Expand Up @@ -359,14 +361,15 @@ _conceptually_ to vscode’s `launch.json` file.
name = 'Development', -- an arbitrary name that you provide so you can recognise this config
flavor = 'DevFlavor', -- your flavour
target = 'lib/main_dev.dart', -- your target
cwd = 'example', -- the working directory for the project. Optional, defaults to the LSP root directory.
device = 'pixel6pro', -- the device ID, which you can get by running `flutter devices`
dart_define = {
API_URL = 'https://dev.example.com/api',
IS_DEV = true,
},
pre_run_callback = nil, -- optional callback to run before the configuration
-- exposes a table containing name, target, flavor and device in the arguments
dart_define_from_file = 'config.json' -- the path to a JSON configuration file
dart_define_from_file = 'config.json', -- the path to a JSON configuration file
},
{
name = 'Web',
Expand Down
30 changes: 29 additions & 1 deletion lua/flutter-tools/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,34 @@ local function get_device_from_args(args)
end
end

local function 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

---@param project_conf flutter.ProjectConfig?
local function get_cwd(project_conf)
if project_conf and project_conf.cwd then
local resolved_path = get_absolute_path(project_conf.cwd)
if not vim.loop.fs_stat(resolved_path) then
return ui.notify("Provided cwd does not exist: " .. resolved_path, ui.ERROR)
end
return resolved_path
end
return lsp.get_lsp_root_dir()
end

---@param opts RunOpts
---@param project_conf flutter.ProjectConfig?
local function run(opts, project_conf)
Expand All @@ -188,7 +216,7 @@ local function run(opts, project_conf)
end
end
runner = use_debugger_runner() and debugger_runner or job_runner
runner:run(paths, args, lsp.get_lsp_root_dir(), on_run_data, on_run_exit)
runner:run(paths, args, get_cwd(project_conf), on_run_data, on_run_exit)
end)
end

Expand Down
3 changes: 1 addition & 2 deletions lua/flutter-tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local utils = lazy.require("flutter-tools.utils") ---@module "flutter-tools.util
---@field dart_define_from_file? string
---@field flutter_mode? string
---@field web_port? number
---@field cwd? string full path of current working directory, defaults to LSP root

local M = {}

Expand Down Expand Up @@ -92,7 +93,6 @@ local config = {
dartSdkPath = paths.dart_sdk,
flutterSdkPath = paths.flutter_sdk,
program = "${workspaceFolder}/lib/main.dart",
cwd = "${workspaceFolder}",
},
{
type = "dart",
Expand All @@ -101,7 +101,6 @@ local config = {
dartSdkPath = paths.dart_sdk,
flutterSdkPath = paths.flutter_sdk,
program = "${workspaceFolder}/lib/main.dart",
cwd = "${workspaceFolder}",
},
}
end,
Expand Down
1 change: 0 additions & 1 deletion lua/flutter-tools/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ function M.setup(config)
name = "Launch Dart",
dartSdkPath = paths.dart_sdk,
program = "${workspaceFolder}/bin/main.dart",
cwd = "${workspaceFolder}",
},
}
end
Expand Down
2 changes: 1 addition & 1 deletion lua/flutter-tools/runners/debugger_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function DebuggerRunner:run(paths, args, cwd, on_run_data, on_run_exit)
function(launch_config)
if not launch_config then return end
launch_config = vim.deepcopy(launch_config)
launch_config.cwd = launch_config.cwd or cwd
if not launch_config.cwd then launch_config.cwd = cwd end
launch_config.args = vim.list_extend(launch_config.args or {}, args or {})
launch_config.dartSdkPath = paths.dart_sdk
launch_config.flutterSdkPath = paths.flutter_sdk
Expand Down

0 comments on commit 7efc0d8

Please sign in to comment.