Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ using `lazy.nvim`
lazy = false,
dependencies = {
'nvim-lua/plenary.nvim',
'stevearc/dressing.nvim', -- optional for vim.ui.select
},
config = true,
}
Expand All @@ -67,7 +66,6 @@ using `vim.pack` (built into Neovim 0.12+, see `:h vim.pack`)
```lua
vim.pack.add({
'https://github.com/nvim-lua/plenary.nvim',
'https://github.com/stevearc/dressing.nvim', -- optional for vim.ui.select
'https://github.com/nvim-flutter/flutter-tools.nvim',
})

Expand All @@ -77,8 +75,10 @@ require('flutter-tools').setup {}
This plugin depends on [plenary.nvim](https://github.com/nvim-lua/plenary.nvim), please make sure it is installed.

This plugin depends on `vim.ui.select` which allows users to control what UI is used for selecting
from a list of options. If you don't have a UI configured for `vim.ui.select` then I highly recommend
the excellent [dressing.nvim](https://github.com/stevearc/dressing.nvim).
from a list of options. If you don't have a UI configured for `vim.ui.select`, pickers such as
[snacks.nvim](https://github.com/folke/snacks.nvim), [fzf-lua](https://github.com/ibhagwan/fzf-lua),
[mini.pick](https://github.com/echasnovski/mini.pick) or
[telescope-ui-select](https://github.com/nvim-telescope/telescope-ui-select.nvim) can provide one.

## Warning

Expand Down Expand Up @@ -143,6 +143,8 @@ require("flutter-tools").setup {} -- use defaults

# Usage

- `FlutterCommands` - Pick a flutter-tools command from a list, shown with your `vim.ui.select` UI (e.g. snacks.nvim, fzf-lua or mini.pick).
- `FlutterFvm` - Pick a Flutter SDK installed with [fvm](https://fvm.app/) and switch the project to it.
- `FlutterRun` - Run the current project. Respects `config.debugger.enabled` setting.
- `FlutterDebug` - Force run current project in debug mode.
- `FlutterDevices` - Brings up a list of connected devices to select from.
Expand Down Expand Up @@ -474,6 +476,7 @@ see `:h statusline` for more information on how to create a statusline.
![telescope picker](https://user-images.githubusercontent.com/22454918/113897929-495a3e80-97c3-11eb-959f-9574319cd93c.png)

You can list available commands in this plugin using [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim).
`FlutterCommands` shows the same list with whatever `vim.ui.select` UI you use.

In order to set this up, you can explicitly load the extension.

Expand All @@ -485,14 +488,6 @@ Or alternatively telescope can lazy load extension but the `Telescope` command w

This can be accessed using `Telescope flutter commands` or `require('telescope').extensions.flutter.commands()`

#### FVM

![telescope fvm](https://user-images.githubusercontent.com/35163478/137667084-98c00c4b-ff8c-4d1e-869e-d2d51cf86f7e.png)

If you have [fvm](https://fvm.app/) installed and enabled in your config, you can change your Flutter SDK via a Telescope picker.

This can be accessed using `Telescope flutter fvm` or `require('telescope').extensions.flutter.fvm()`

## Debugging

_Requires nvim-dap_
Expand Down
3 changes: 3 additions & 0 deletions lua/flutter-tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local dap = lazy.require("flutter-tools.dap") ---@module "flutter-tools.dap"
local decorations = lazy.require("flutter-tools.decorations") ---@module "flutter-tools.decorations"
local guides = lazy.require("flutter-tools.guides") ---@module "flutter-tools.guides"
local log = lazy.require("flutter-tools.log") ---@module "flutter-tools.log"
local menu = lazy.require("flutter-tools.menu") ---@module "flutter-tools.menu"
local lsp = lazy.require("flutter-tools.lsp") ---@module "flutter-tools.lsp"
local outline = lazy.require("flutter-tools.outline") ---@module "flutter-tools.outline"
local devices = lazy.require("flutter-tools.devices") ---@module "flutter-tools.devices"
Expand All @@ -34,6 +35,8 @@ local function setup_commands()
command("FlutterVisualDebug", commands.visual_debug)
command("FlutterChangeTargetPlatform", commands.change_target_platform)
command("FlutterToggleBrightness", commands.brightness)
command("FlutterCommands", menu.select_command)
command("FlutterFvm", menu.select_fvm)
-- Lists
command("FlutterDevices", devices.list_devices)
command("FlutterEmulators", devices.list_emulators)
Expand Down
82 changes: 44 additions & 38 deletions lua/flutter-tools/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ local commands = lazy.require("flutter-tools.commands") ---@module "flutter-tool
local ui = lazy.require("flutter-tools.ui") ---@module "flutter-tools.ui"

---@alias TelescopeEntry {hint: string, label: string, command: fun(), id: integer}
---@alias CustomOptions {title: string, callback: fun(bufnr: integer)}
---@alias CustomOptions {title: string}

local M = {}

-- Accounts for the vertical padding implicit in the dropdown.
local MENU_PADDING = 4

local function run(cmd, ...)
if not cmd then return end
local success, msg = pcall(cmd, ...)
if not success then ui.notify(msg, ui.ERROR) end
end

local function execute_command(bufnr)
local selection = action_state.get_selected_entry()
actions.close(bufnr)
local cmd = selection.command
if cmd then
local success, msg = pcall(cmd)
if not success then ui.notify(msg, ui.ERROR) end
end
run(selection.command)
end

local function command_entry_maker(max_width)
Expand Down Expand Up @@ -65,7 +67,6 @@ end
---@param opts CustomOptions
---@return table
local function picker_opts(items, opts)
local callback = opts.callback or execute_command
return {
prompt_title = opts.title,
finder = finders.new_table({
Expand All @@ -74,8 +75,8 @@ local function picker_opts(items, opts)
}),
sorter = sorters.get_generic_fuzzy_sorter(),
attach_mappings = function(_, map)
map("i", "<CR>", callback)
map("n", "<CR>", callback)
map("i", "<CR>", execute_command)
map("n", "<CR>", execute_command)
-- If the return value of `attach_mappings` is true, then the other
-- default mappings are still applies.
-- Return false if you don't want any other mappings applied.
Expand All @@ -97,7 +98,8 @@ function M.get_config(items, user_opts, opts)
}))
end

function M.commands(opts)
---@return TelescopeEntry[]
local function command_items()
local cmds = {}

if commands.is_running() then
Expand Down Expand Up @@ -207,7 +209,7 @@ function M.commands(opts)
},
{
id = "flutter-tools-generate",
label = "Generate ",
label = "Generate",
hint = "Generate code",
command = commands.generate,
},
Expand Down Expand Up @@ -265,45 +267,49 @@ function M.commands(opts)
})
end

pickers.new(M.get_config(cmds, opts, { title = "Flutter tools commands" })):find()
return cmds
end

local function execute_fvm_use(bufnr)
local selection = action_state.get_selected_entry()
actions.close(bufnr)
local cmd = selection.command
if cmd then
local success, msg = pcall(cmd, selection.ordinal)
if not success then ui.notify(msg, ui.ERROR) end
function M.commands(opts)
pickers.new(M.get_config(command_items(), opts, { title = "Flutter tools commands" })):find()
end

---@param items TelescopeEntry[]
---@param prompt string
---@param on_choice fun(item: TelescopeEntry)
local function select(items, prompt, on_choice)
local width = 0
for _, item in ipairs(items) do
width = math.max(width, vim.api.nvim_strwidth(item.label))
end
vim.ui.select(items, {
prompt = prompt,
kind = "flutter-tools",
format_item = function(item)
if not item.hint or item.hint == "" then return item.label end
local padding = string.rep(" ", width - vim.api.nvim_strwidth(item.label))
return item.label .. padding .. " • " .. item.hint
end,
}, function(item)
if item then on_choice(item) end
end)
end

function M.fvm(opts)
commands.fvm_list(function(sdks)
opts = opts and not vim.tbl_isempty(opts) and opts
or themes.get_dropdown({
previewer = false,
layout_config = {
height = #sdks + MENU_PADDING,
},
})
function M.select_command()
select(command_items(), "Flutter tools commands", function(item) run(item.command) end)
end

local sdk_entries = {}
function M.select_fvm()
commands.fvm_list(function(sdks)
local items = {}
for _, sdk in pairs(sdks) do
table.insert(sdk_entries, {
table.insert(items, {
id = sdk.name,
label = sdk.name,
hint = sdk.dart_sdk_version and "(Dart SDK " .. sdk.dart_sdk_version .. ")" or "",
command = commands.fvm_use,
})
end

pickers
.new(M.get_config(sdk_entries, opts, {
title = "Change Flutter SDK",
callback = execute_fvm_use,
}))
:find()
select(items, "Change Flutter SDK", function(item) run(commands.fvm_use, item.id) end)
end)
end

Expand Down
39 changes: 0 additions & 39 deletions lua/flutter-tools/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,43 +95,6 @@ end
---@param on_confirm function
M.input = function(opts, on_confirm) vim.ui.input(opts, on_confirm) end

--- @param items SelectionEntry[]
--- @param title string
--- @param on_select fun(item: SelectionEntry)
local function get_telescope_picker_config(items, title, on_select)
local ok = pcall(require, "telescope")
if not ok then return end

local filtered = vim.tbl_filter(function(value) return value.data ~= nil end, items) --[[@as SelectionEntry[]]

return require("flutter-tools.menu").get_config(
vim.tbl_map(function(item)
local data = item.data
if item.type == entry_type.CODE_ACTION then
return {
id = data.title,
label = data.title,
command = function() on_select(data) end,
}
elseif item.type == entry_type.DEVICE then
return {
id = data.id,
label = data.name,
hint = data.platform,
command = function() on_select(data) end,
}
elseif item.type == entry_type.INFO then
return {
id = item.text,
label = item.text,
command = function() end,
}
end
end, filtered),
{ title = title }
)
end

---@alias PopupOpts {title:string, lines: SelectionEntry[], on_select: fun(item: SelectionEntry)}
---@param opts PopupOpts
function M.select(opts)
Expand All @@ -143,8 +106,6 @@ function M.select(opts)
prompt = title,
kind = "flutter-tools",
format_item = function(item) return item.text end,
-- custom key for dressing.nvim
telescope = get_telescope_picker_config(lines, title, on_select),
}, function(item)
if not item or item.data == nil then return end
on_select(item.data)
Expand Down
1 change: 0 additions & 1 deletion lua/telescope/_extensions/flutter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ end
return telescope.register_extension({
exports = {
commands = menu.commands,
fvm = menu.fvm,
},
})
Loading
Loading