Advanced services for integrating dprint with code editors through Language Server Protocol (LSP) and background services. These services enable real-time formatting, configuration management, and seamless editor workflows.
Full LSP server implementation providing real-time formatting and configuration management for editor integrations.
# Start LSP server
dprint lsp
# LSP server provides:
# - textDocument/formatting
# - textDocument/rangeFormatting
# - workspace/configuration
# - workspace/didChangeConfigurationUsage Examples:
# Start LSP server (typically called by editors)
dprint lsp
# LSP communication over stdin/stdout
# Editor sends JSON-RPC messages
# dprint responds with formatting results
# VS Code integration example:
# Extension calls: dprint lsp
# Then communicates via LSP protocolLSP Capabilities:
{
"textDocumentSync": "full",
"documentFormattingProvider": true,
"documentRangeFormattingProvider": true,
"configurationProvider": true
}Background service for editor integrations with parent process monitoring and optimized performance.
# Start editor service with parent PID monitoring
dprint editor-service <PARENT_PID>Usage Examples:
# Start editor service (VS Code example)
dprint editor-service 12345
# Service behavior:
# 1. Monitors parent process (PID 12345)
# 2. Automatically shuts down if parent exits
# 3. Maintains formatting state and cache
# 4. Provides fast formatting responsesService Features:
Outputs structured information for editor integrations including supported features and configuration.
# Output editor integration information
dprint editor-infoUsage Examples:
# Get editor integration information
dprint editor-info
# Example JSON output:
{
"schemaVersion": 3,
"cliVersion": "0.50.1",
"configSchemaUrl": "https://dprint.dev/schemas/v0.json",
"plugins": [
{
"name": "typescript",
"version": "0.88.0",
"configKey": "typescript",
"fileExtensions": ["ts", "tsx", "js", "jsx"],
"fileNames": [],
"associatedFiles": ["tsconfig.json", "jsconfig.json"]
}
]
}Full document formatting via LSP protocol.
{
"method": "textDocument/formatting",
"params": {
"textDocument": {
"uri": "file:///path/to/file.ts"
},
"options": {
"tabSize": 2,
"insertSpaces": true
}
}
}Format specific ranges within documents.
{
"method": "textDocument/rangeFormatting",
"params": {
"textDocument": {
"uri": "file:///path/to/file.ts"
},
"range": {
"start": {"line": 10, "character": 0},
"end": {"line": 20, "character": 0}
},
"options": {
"tabSize": 2,
"insertSpaces": true
}
}
}Dynamic configuration updates via LSP.
{
"method": "workspace/didChangeConfiguration",
"params": {
"settings": {
"dprint": {
"config": "/path/to/dprint.json",
"plugins": ["typescript", "json"]
}
}
}
}// settings.json configuration
{
"dprint.path": "/path/to/dprint",
"dprint.config": "./dprint.json",
"editor.defaultFormatter": "dprint.dprint",
"editor.formatOnSave": true,
"editor.formatOnType": true,
"dprint.verbose": false
}-- LSP configuration (Neovim)
local lspconfig = require('lspconfig')
lspconfig.dprint.setup {
cmd = { "dprint", "lsp" },
filetypes = { "typescript", "javascript", "json", "markdown" },
root_dir = lspconfig.util.root_pattern("dprint.json", ".git"),
settings = {
dprint = {
config = "./dprint.json"
}
}
};; LSP integration (lsp-mode)
(use-package lsp-mode
:hook ((typescript-mode js-mode json-mode markdown-mode) . lsp)
:config
(lsp-register-client
(make-lsp-client
:new-connection (lsp-stdio-connection '("dprint" "lsp"))
:major-modes '(typescript-mode js-mode json-mode markdown-mode)
:server-id 'dprint)))# Editor service advantages:
# - Persistent plugin state (faster subsequent formats)
# - Configuration caching
# - Reduced startup overhead
# - Memory sharing across format requests{
"dprint": {
"incremental": true, // Enable incremental formatting
"cacheSize": "100mb", // Plugin cache size
"timeout": 30000, // Format timeout (ms)
"maxFileSize": "1mb" // Skip large files
}
}# Service memory optimization
export DPRINT_MAX_MEMORY=512mb
# Plugin cache optimization
export DPRINT_CACHE_SIZE=100mb
# Garbage collection tuning
export DPRINT_GC_INTERVAL=300s# Debug LSP communication
dprint lsp --log-level debug
# Check LSP server status
ps aux | grep "dprint lsp"
# Test LSP manually
echo '{"jsonrpc":"2.0","method":"initialize","params":{},"id":1}' | dprint lsp# Check service process
ps aux | grep "dprint editor-service"
# Debug service startup
dprint editor-service $PPID --log-level debug
# Monitor parent process
ps -p $PPID # Check if parent process exists# Verify editor info output
dprint editor-info | jq '.'
# Check configuration resolution
dprint output-resolved-config
# Test file formatting directly
dprint fmt --stdin test.ts < test_file.ts# Plugin not found
{
"error": "Plugin 'typescript' not found",
"solution": "Run 'dprint config add typescript'"
}
# Configuration error
{
"error": "Failed to resolve config",
"solution": "Check dprint.json syntax and plugin URLs"
}
# File too large
{
"error": "File exceeds maximum size",
"solution": "Increase maxFileSize or skip large files"
}// settings.json
{
"dprint.path": "/usr/local/bin/dprint",
"dprint.config": "./dprint.json",
"editor.defaultFormatter": "dprint.dprint",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.dprint": true
},
"[typescript]": {
"editor.defaultFormatter": "dprint.dprint"
},
"[javascript]": {
"editor.defaultFormatter": "dprint.dprint"
}
}-- init.lua
local lspconfig = require('lspconfig')
-- dprint LSP setup
lspconfig.dprint.setup {
cmd = { "dprint", "lsp" },
filetypes = { "typescript", "javascript", "json", "markdown", "rust" },
root_dir = lspconfig.util.root_pattern("dprint.json", "dprint.jsonc", ".git"),
init_options = {
config = "./dprint.json"
},
on_attach = function(client, bufnr)
-- Enable formatting
if client.server_capabilities.documentFormattingProvider then
vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>f", "<cmd>lua vim.lsp.buf.format()<CR>", {})
end
end
}
-- Auto-format on save
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = {"*.ts", "*.js", "*.json", "*.md", "*.rs"},
callback = function()
vim.lsp.buf.format({ timeout_ms = 2000 })
end
})