LSP textDocument/formatting appends result instead of replacing range

Hi again,

one of the lsp extensions I’m working on has document formatting capabilities, but it seems that Nova doesn’t respect the range in the response TextEdit struct and fully inserts the result to the existing document at 0,0 - essentially duplicating the text in the editor.

here is a log the rpc exchange in that case:

Lua Language Server[14:57:39.949000] Sending JSON-RPC request: number(129) textDocument/formatting
{
  "method" : "textDocument\/formatting",
  "id" : 129,
  "jsonrpc" : "2.0",
  "params" : {
	"textDocument" : {
	  "uri" : "file:\/\/\/Users\/olifink\/Courses\/pickaxe\/others\/hello.lua"
	},
	"options" : {
	  "trimTrailingWhitespace" : false,
	  "tabSize" : 4,
	  "insertFinalNewline" : false,
	  "insertSpaces" : false
	}
  }
}

Lua Language Server[14:57:39.950000] Received JSON-RPC response: number(129) textDocument/formatting
{
  "jsonrpc" : "2.0",
  "result" : [
	{
	  "newText" : "local function hello(name)\n\tprint(\"Hello \", name)\nend\n\nlocal function again(what)\n\tprint(\"Nobody here \", what)\nend\nfor i, v in ipairs(arg) do\n\tprint(i, v)\nend\n\nprint(\"pwd\", os.getenv(\"PWD\"))\nprint(table.concat(arg, ','))\nhello(\"Lua\")\n",
	  "range" : {
		"end" : {
		  "line" : 14,
		  "character" : 0
		},
		"start" : {
		  "character" : 0,
		  "line" : 0
		}
	  }
	}
  ],
  "id" : 129
}

Lua Language Server[14:57:39.950000] Sending JSON-RPC notification: textDocument/didChange
{
  "jsonrpc" : "2.0",
  "method" : "textDocument\/didChange",
  "params" : {
	"textDocument" : {
	  "uri" : "file:\/\/\/Users\/olifink\/Courses\/pickaxe\/others\/hello.lua",
	  "version" : 15
	},
	"contentChanges" : [
	  {
		"text" : "local function hello(name)\n\tprint(\"Hello \", name)\nend\n\nlocal function again(what)\n\tprint(\"Nobody here \", what)\nend\nfor i, v in ipairs(arg) do\n\tprint(i, v)\nend\n\nprint(\"pwd\", os.getenv(\"PWD\"))\nprint(table.concat(arg, ','))\nhello(\"Lua\")\n",
		"range" : {
		  "end" : {
			"character" : 0,
			"line" : 0
		  },
		  "start" : {
			"line" : 0,
			"character" : 0
		  }
		}
	  }
	]
  }
}

can you perhaps look into that? is there any reason the ranges don’t match for these requests/responses?

O

Thank you for the report. I will investigate.

maybe it’s helpful to have another example from a different LSP with the same problem:

Ruby Language Server[17:43:06.480000] Sending JSON-RPC request: number(24) textDocument/formatting
{
  "id" : 24,
  "jsonrpc" : "2.0",
  "method" : "textDocument\/formatting",
  "params" : {
	"options" : {
	  "insertSpaces" : false,
	  "insertFinalNewline" : false,
	  "tabSize" : 4,
	  "trimTrailingWhitespace" : false
	},
	"textDocument" : {
	  "uri" : "file:\/\/\/Users\/olifink\/Code\/try\/ruby\/simple\/start.rb"
	}
  }
}

Ruby Language Server[17:43:06.562000] Received JSON-RPC response: number(24) textDocument/formatting
{
  "id" : 24,
  "result" : [
	{
	  "range" : {
		"start" : {
		  "line" : 0,
		  "character" : 0
		},
		"end" : {
		  "line" : 69,
		  "character" : 69
		}
	  },
	  "newText" : "class Start\n  def thing\n    puts 'Hello'\n  end\nend\n\ns = Start.new\ns.thing\n"
	}
  ],
  "jsonrpc" : "2.0"
}

Ruby Language Server[17:43:06.562000] Sending JSON-RPC notification: textDocument/didChange
{
  "jsonrpc" : "2.0",
  "method" : "textDocument\/didChange",
  "params" : {
	"contentChanges" : [
	  {
		"text" : "class Start\n  def thing\n    puts 'Hello'\n  end\nend\n\ns = Start.new\ns.thing\n",
		"range" : {
		  "end" : {
			"line" : 0,
			"character" : 0
		  },
		  "start" : {
			"character" : 0,
			"line" : 0
		  }
		}
	  }
	],
	"textDocument" : {
	  "version" : 47,
	  "uri" : "file:\/\/\/Users\/olifink\/Code\/try\/ruby\/simple\/start.rb"
	}
  }
}

let me know if you need more information