Seperate flutter and lsp but has log color issue

This commit is contained in:
2026-04-17 14:33:42 +02:00
parent 0372e79b79
commit c8d4ea3cd1
4 changed files with 164 additions and 62 deletions

121
lua/plugins/flutter.lua Normal file
View File

@@ -0,0 +1,121 @@
return {
{
"nvim-flutter/flutter-tools.nvim",
lazy = false,
dependencies = {
"nvim-lua/plenary.nvim",
"stevearc/dressing.nvim",
"m00qek/baleia.nvim",
},
config = function()
vim.opt.termguicolors = true
vim.env.FLUTTER_FORCE_COLOR = "1"
local baleia = require("baleia").setup({
strip_ansi_codes = true,
})
vim.api.nvim_create_autocmd("BufWinEnter", {
pattern = "*__FLUTTER_DEV_LOG__*",
callback = function(args)
local bufnr = args.buf
if not vim.bo[bufnr].modifiable then
vim.bo[bufnr].modifiable = true
end
baleia.automatically(bufnr)
end,
})
vim.api.nvim_create_autocmd("OptionSet", {
pattern = "modifiable",
callback = function(args)
local bufnr = args.buf
if not vim.bo[bufnr].modifiable then
vim.bo[bufnr].modifiable = true
end
end,
})
require("flutter-tools").setup({
ui = {
border = "rounded",
notification_style = "plugin",
},
decorations = {
statusline = {
app_version = true,
device = true,
}
},
lsp = {
capabilities = require("cmp_nvim_lsp").default_capabilities(),
settings = {
showTodos = true,
completeFunctionCalls = true,
updateImportsOnRename = true,
},
},
widget_guides = {
enabled = true,
},
dev_log = {
enabled = true,
notify_errors = false,
open_cmd = "vertical rightbelow vsplit",
},
})
-- Some Flutter log lines include ANSI start codes but no explicit reset.
-- Baleia keeps style state across lines, so missing reset can "bleed"
-- colors into following lines. Normalize those log lines first.
do
local ok, flutter_log = pcall(require, "flutter-tools.log")
if ok and flutter_log and type(flutter_log.log) == "function" then
local raw_log = flutter_log.log
local ansi_pattern = "\27%[[0-9;]*m"
local ansi_reset = "\27[0m"
flutter_log.log = function(data)
if type(data) == "string" then
local has_ansi = data:find(ansi_pattern) ~= nil
local has_reset = data:find("\27%[0m") ~= nil
if has_ansi and not has_reset then
data = data .. ansi_reset
end
end
raw_log(data)
end
end
end
-- Custom Shorthand Commands
local user_command = vim.api.nvim_create_user_command
-- Simple Aliases
-- user_command("FRun", "FlutterRun", {})
-- user_command("FRun", "FlutterRun --color", {})
user_command("FRun", "FlutterRun --color", {})
user_command("FReload", "FlutterReload", {})
user_command("FRestart", "FlutterRestart", {})
user_command("FQuit", "FlutterQuit", {})
user_command("FDevices", "FlutterDevices", {})
user_command("FEmulators", "FlutterEmulators", {})
user_command("FLogs", "FlutterLogToggle", {})
user_command("FLogsClear", "FlutterLogClear", {})
-- Target-specific Run Command
user_command("FRunT", function(opts)
vim.cmd("FlutterRun --color --target=" .. opts.args)
end, {
nargs = 1,
complete = "file",
desc = "Run Flutter with a specific target file",
})
end,
},
}

View File

@@ -8,10 +8,12 @@ return {
},
config = function()
require("mason").setup()
local mason_lspconfig = require("mason-lspconfig")
-- We no longer need: local lspconfig = require("lspconfig")
local capabilities = require("cmp_nvim_lsp").default_capabilities()
-- Register global capabilities for all servers
vim.lsp.config("*", {
capabilities = require("cmp_nvim_lsp").default_capabilities(),
})
local servers = {
"lua_ls",
@@ -31,41 +33,36 @@ return {
ensure_installed = servers,
})
-- Neovim 0.11+ approach: Use vim.lsp.config
-- Neovim 0.11+ approach: Configure and Enable
for _, server_name in ipairs(servers) do
local config = {
capabilities = capabilities,
}
if server_name == "lua_ls" then
config.settings = {
Lua = {
diagnostics = { globals = { "vim" } },
vim.lsp.config("lua_ls", {
settings = {
Lua = {
diagnostics = { globals = { "vim" } },
},
},
}
})
end
-- Enable the server configuration natively
vim.lsp.config(server_name, config)
-- Activate the server
vim.lsp.enable(server_name)
end
-- Diagnostic display config
vim.diagnostic.config({
virtual_text = true, -- show error inline at end of line
virtual_text = true,
signs = true,
underline = true,
update_in_insert = false, -- only update after leaving insert mode
float = {
border = "rounded",
source = true, -- shows which LSP reported the error
},
update_in_insert = false,
float = { border = "rounded", source = true },
})
-- Keybindings (unchanged)
-- Keybindings & Formatting
vim.api.nvim_create_autocmd("LspAttach", {
callback = function(ev)
local opts = { buffer = ev.buf }
-- Neovim 0.11+ provides some defaults like 'grn' (rename),
-- but keeping your custom ones is fine.
vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
vim.keymap.set("n", "<C-k>", vim.lsp.buf.hover, opts)
vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
@@ -78,8 +75,7 @@ return {
vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts) -- jump to next error
vim.keymap.set("n", "<leader>el", vim.diagnostic.setloclist, opts) -- all errors in quickfix list
vim.keymap.set("n", "<leader>af", vim.lsp.buf.format, { desc = "Format file" }) -- manual format
-- Format on save
-- Format on save (with filter to prevent multi-client conflicts)
vim.api.nvim_create_autocmd("BufWritePre", {
buffer = ev.buf,
callback = function()
@@ -90,34 +86,13 @@ return {
})
end,
},
{
"nvim-flutter/flutter-tools.nvim",
lazy = false, -- Load on startup to ensure it handles dartls correctly
dependencies = {
"nvim-lua/plenary.nvim",
"stevearc/dressing.nvim", -- Optional but highly recommended for better UI
},
config = function()
require("flutter-tools").setup({
lsp = {
-- Pass your existing capabilities to the flutter-tools LSP config
capabilities = require("cmp_nvim_lsp").default_capabilities(),
-- You can add specific dartls settings here if needed
settings = {
showTodos = true,
completeFunctionCalls = true,
},
},
})
end,
},
{
"hrsh7th/nvim-cmp",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-path",
"L3MON4D3/LuaSnip", -- Snippet engine is usually required by cmp configs
"L3MON4D3/LuaSnip",
"saadparwaiz1/cmp_luasnip",
},
config = function()
@@ -129,11 +104,9 @@ return {
end,
},
mapping = cmp.mapping.preset.insert({
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(),
["<CR>"] = cmp.mapping.confirm({ select = true }),
["<Tab>"] = cmp.mapping(function(fallback) -- add this
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.confirm({ select = true })
else
@@ -146,6 +119,7 @@ return {
{ name = "luasnip" },
}, {
{ name = "buffer" },
{ name = "path" },
}),
})
end,