Comments (3)
Here's a command I made for this, maybe it could be used a reference for a future PR:
vim.api.nvim_create_user_command('DiffFormat', function()
local lines = vim.fn.system('git diff --unified=0'):gmatch('[^\n\r]+')
local ranges = {}
for line in lines do
if line:find('^@@') then
local line_nums = line:match('%+.- ')
if line_nums:find(',') then
local _, _, first, second = line_nums:find('(%d+),(%d+)')
table.insert(ranges, {
start = { tonumber(first), 0 },
['end'] = { tonumber(first) + tonumber(second), 0 },
})
else
local first = tonumber(line_nums:match('%d+'))
table.insert(ranges, {
start = { first, 0 },
['end'] = { first + 1, 0 },
})
end
end
end
local format = require('conform').format
for _, range in pairs(ranges) do
format {
range = range,
}
end
end, { desc = 'Format changed lines' })
from conform.nvim.
conform supports range formatting, so you could probably re-use a lot of the code in the injected formatter and pass in git diff ranges instead of treesitter ranges
from conform.nvim.
I see!
I would only add that - if this feature gets implemented - it may make sense to enable this on a per-formatter basis.
For eg. I would only want golines
to take effect in lines I've changed, but it would make sense to have goimports
trigger every time I change a file as for the order of the imports to be correct, the whole context (all of the imports) need to be passed into the formatter - not just the import lines that were changed.
Thanks for taking this feature request into consideration :)
from conform.nvim.
Related Issues (20)
- Question: range formatting on lsp fallback formatters HOT 1
- bug: phpcbf overwrites php-cs-fixer config with stats HOT 3
- Question/Bug: I can't get conform to format my code properly with eslint/prettier. HOT 3
- question: how to execute an external command for formatting? HOT 1
- bug: docs contain extra tag `*required*` HOT 1
- feature request: ability to disable formatter completely at runtime HOT 3
- Question: How to make it work with ruby?
- bug: Formatter 'prettier' timeout - when using plugins HOT 1
- bug: Latex in Mardown doesn't format HOT 1
- Question: picking the correct formatter when multiple formatters exist within a monorepo HOT 2
- bug: dart_format does not work HOT 1
- conform doesn't seem to recognize any Mason installed formatter HOT 2
- Unicode Symbols replaced with `?` on Windows HOT 2
- bug: Conform inserting blank line at end of files after running prettier HOT 1
- bug: astyle windows range formatting HOT 1
- feature request: Add yew-fmt to available formatters
- feature request: prefer LSP and fallback on formatters HOT 2
- feature request: Add stylish-haskell to builtin formatters HOT 2
- Question: How to print error message with conform.nvim HOT 2
- bug: rust async code formatting error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from conform.nvim.