I was looking for a solution similar to CoC's coc-fix-current but using native lsp for Neovim 0.5 but I did not find such thing in the documentation, is there any way to achieve this through other method?
I had this problem and by hacking up the code that the telescope.nvim plugin uses to list and run code actions, came up with this monstrosity:
local function run_action(action, offse)
if action.edit or type(action.command) == "table" then
if action.edit then
vim.lsp.util.apply_workspace_edit(action.edit, offse)
end
if type(action.command) == "table" then
vim.lsp.buf.execute_command(action.command)
end
else
vim.lsp.buf.execute_command(action)
end
end
local function do_action(action, client)
if
not action.edit
and client
and type(client.resolved_capabilities.code_action) == "table"
and client.resolved_capabilities.code_action.resolveProvider
then
client.request("codeAction/resolve", action, function(err, real)
if err then
return
end
if real then
run_action(real, client.offset_encoding)
else
run_action(action, client.offset_encoding)
end
end)
else
run_action(action, client.offset_encoding)
end
end
return function()
local params = vim.lsp.util.make_range_params() -- get params for current position
params.context = {
diagnostics = vim.lsp.diagnostic.get_line_diagnostics(),
only = {"quickfix"}
}
local results, err = vim.lsp.buf_request_sync(
0, -- current buffer
"textDocument/codeAction", -- get code actions
params,
900
)
if err then return end
if not results or vim.tbl_isempty(results) then
print "No quickfixes!"
return
end
-- we have an action!
for cid, resp in pairs(results) do
if resp.result then
for _, result in pairs(resp.result) do
-- this is the first action, run it
do_action(result, vim.lsp.get_client_by_id(cid))
return
end
end
end
print "No quickfixes!"
end
Since it's lua, you'll need to place it in a .lua file somewhere that nvim searches for modules (for example, as ~/.config/nvim/lua/lsp_fixcurrent.lua) and then bind to :lua require("lsp_fixcurrent")()
As of neovim 0.8, thanks to this PR, there's an apply boolean which does just that.
To make sure you only apply relevant fixes, you can use the filter attribute and look for the "prefered" fixes.
Here's what I've put in my config :
local opts = { noremap=true, silent=true }
local function quickfix()
vim.lsp.buf.code_action({
filter = function(a) return a.isPreferred end,
apply = true
})
end
vim.keymap.set('n', '<leader>qf', quickfix, opts)
mincrmatt12 answer, updated for newer neovim (0.8?) which complains client.resolved_capabilities should not be used.
local function run_action(action, offse)
if action.edit or type(action.command) == "table" then
if action.edit then
vim.lsp.util.apply_workspace_edit(action.edit, offse)
end
if type(action.command) == "table" then
vim.lsp.buf.execute_command(action.command)
end
else
vim.lsp.buf.execute_command(action)
end
end
local function do_action(action, client)
if
not action.edit
and client
and type(client.server_capabilities) == "table"
and client.server_capabilities.resolveProvider
then
client.request("codeAction/resolve", action, function(err, real)
if err then
return
end
if real then
run_action(real, client.offset_encoding)
else
run_action(action, client.offset_encoding)
end
end)
else
run_action(action, client.offset_encoding)
end
end
return function()
local params = vim.lsp.util.make_range_params() -- get params for current position
params.context = {
diagnostics = vim.lsp.diagnostic.get_line_diagnostics(),
only = { "quickfix" },
}
local results, err = vim.lsp.buf_request_sync(
0, -- current buffer
"textDocument/codeAction", -- get code actions
params,
900
)
if err then
return
end
if not results or vim.tbl_isempty(results) then
print("No quickfixes!")
return
end
-- we have an action!
for cid, resp in pairs(results) do
if resp.result then
for _, result in pairs(resp.result) do
-- this is the first action, run it
do_action(result, vim.lsp.get_client_by_id(cid))
return
end
end
end
print("No quickfixes!")
end
Maybe you are looking for: vim.lsp.buf.code_action()?
Related
So what I've been trying is to Save a certain "Text" to the DataStore from a TextLabel, The Data Saves Successfully, But when trying to load it gives me just gives me a "Failed", Any Help?
Here's a Quick Video: https://www.youtube.com/watch?v=W-J6U8zmATk&feature=youtu.be
Script:
local DataStoreService = game:GetService("DataStoreService")
local IDStorage = DataStoreService:GetDataStore("IDStorage3")
elseif Player.Team.Name == "Intelligence Agency" then
if Player:IsInGroup(7503826) or Player:GetRankInGroup(7465879) >= 251 then
Rank.User.Text = "[REDACTED]"
Rank.User.Back.Text = "[REDACTED]"
Rank.Rank.TextColor3 = Color3.new(0.827451, 0, 0)
game.ReplicatedStorage.NewID.OnServerEvent:Connect(function(player, playerToID, AssignedID)
if player:IsInGroup(7465879) then
local success, err = pcall(function()
IDStorage:SetAsync(playerToID, AssignedID)
end)
if success then
print("Data Assigned") -- Data Works and Saves
else
warn("Failed to Save")
end
end
end)
local ID = IDStorage:GetAsync(Player)
if ID then
print(ID)
else
warn("Failed") -- Always Returns me this.
Rank.Rank.Text = "0"
end
Rank.User.Text = "[REDACTED]"
Rank.User.Back.Text = "[REDACTED]"
end
The DataStore:GetAsync(key) function expects a string for the key. It looks like you're passing in a Player object and not the player's username, which you said was the key that you were storing the data in.
Try this swapping this line
local ID = IDStorage:GetAsync(Player)
for this instead :
local success, result = pcall(function()
local key = Player.Name
return IDStorage:GetAsync(key)
end
if success then
print("Got Id : ", result)
local ID = result
if ID then
-- do stuff with the result
else
-- looks like a new player with no saved data
end
else
warn("failed to get id with error :", result)
-- do something to handle the error, like retry
end
Something to be careful about when saving data is that usernames can change. If I were to change my name, the next time I joined this game, all of my progress would be gone because the name doesn't match up with the saved key. That is why a player's userId tends to be the recommend key.
whats the difference between class and instance in lua?
I know classes are like the template and the instance is the object created from the template but I am wondering what the difference in code is.
my goal is to make a system that works like this..
--class.widget.lua----------------------------------------------------------
local class = require "class"
local widget = class("class.widget")
function widget:create()
--create variables here
end
function widget:update()
--update variables here
end
function widget:render()
--widget render
end
return widget
--class.widget.button.lua---------------------------------------------------
local class = require "class"
local widget = require "class.widget"
local button = class("button", widget)
function button:create(x, y)
base:create()
--create variables here
end
function button:update()
base:update()
--update variables here
end
function button:render()
base:render()
--widget render
end
return button
--main.lua------------------------------------------------------------------
local button = require "class.widget.button.lua"
button1 = button(0, 0)
button2 = button(0, 16)
even though this hasn't been answered, this right here is working exactly the way I want it to
I am posting it here if anybody wants to use it
EDIT: this is a better version for both me and any one looking for a good lua class
return function(name, base)
local class = {}
--private vars
class.__name = name
class.__base = base
class.__index = class
class.__event = "create"
--events
class.onCreate = function(inst) end
class.onUpdate = function(inst) end
class.onRender = function(inst) end
class.onDelete = function(inst) end
--functions
class.create = function(inst) inst.__event = "create" inst:onCreate() end
class.update = function(inst) inst.__event = "update" inst:onUpdate() end
class.render = function(inst) inst.__event = "render" inst:onRender() end
class.delete = function(inst) inst.__event = "delete" inst:onDelete() end
class.getBase = function(inst) return inst.__base end
class.getName = function(inst) return inst.__name end
class.inheritEvent = function(inst)
if inst.__event == "create" then inst.__base:create() end
if inst.__event == "update" then inst.__base:update() end
if inst.__event == "render" then inst.__base:render() end
if inst.__event == "delete" then inst.__base:delete() end
end
--constructor
local MT = {}
MT.__index = base
function MT:__call(_, ...)
local inst = setmetatable({}, class)
inst:create(inst, ...)
return inst
end
return setmetatable(class, MT)
end
Right now I'm trying to build some code to handle lists over 100 items, as returned by the Amazon API endpoints. This requires building page-support into our data-gathering routines. This is my first time doing much with coffeescript, so I'm running into some conceptual walls here.
In a less async language, what I'm trying to do would be handleable using an until loop:
puts "Fetching launch configs"
next_token = ''
do
if next_token.length > 0
page_list = get_autoscale_configs(next_token)
else
page_list = get_autoscale_configs
if page_list.NextToken is undefined
next_token = ''
else
next_token = page_list.NextToken
until(next_token.length == 0)
The method of doing this in coffeescript is eluding me. What I have now...
populate_configs = ( ) ->
process_results = ( err data ) ->
if err
return err
# do some logic
if data.NextToken
saved.next_token = data.NextToken
else
saved.next_token = ''
return console.log "Finished parsing #{data.LaunchConfigurations.length} items."
if saved.next_token = ''
autoscaling.describeLaunchConfigurations {
MaxRecords: 100, StartToken: next_token
}, ( err, data ) -> process_results( err, data )
else
autoscaling.describeLaunchConfigurations {
MaxRecords: 100
}, ( err, data ) -> process_results( err, data )
And then in the body of the code, this function is invoked:
saved = {}
async.series [
( series_cb ) ->
saved.next_token = ''
async.doWhilst populate_configs,
saved.next_token.length > 4,
( err ) ->
if err
# complain about it.
# else, log success
return series_cb()
# more callbacks
]
The idea here being that populate_configs is called by doWhilst, which then fetches a list of launch_configs out of amazon. The data is then passed into another function called process_results, which persists things that should be persisted and sets variables for next_token. It returns, and doWhilst tests to see if the test is passing (the string-length of saved.next_token is long enough to be data); if it passes, it runs through populate_configs again, if it fails, it runs the third callback.
What I'm getting right now is that the first iteration of that populate_configs block is executed, but then the entire execution stops dead at that point. None of the calls in the error-handler of doWhilst are being executed.
Clearly, I'm misunderstanding how callbacks work and how to get myself out of this hole. This part needs to be synchronous. Once I have this list built, I can do all sorts of async fun with the list I'm building. But I need the list first.
I think the issue is here: if saved.next_token = ''. You set next_token to '' so populate_configs runs only once. The comparaison is done with == or is in CoffeeScript.
Also, ( err, data ) -> process_results( err, data ) can be replaced by process_results.
I am trying to instrument all the if statements in a C program. For example
if(condition)
statement1;
else
statement2;
is instrumented to
if(condition)
{
statement1;
}
else
{
statement2;
}
I have the following code for instrumentation
void MyRecursiveASTVisitor::InstrumentStmt(Stmt *s)
{
// Only perform if statement is not compound
SourceLocation ST = s->getLocStart();
if (!isa<CompoundStmt>(s)) {
//insert an opening brace if not present
Rewrite.InsertText(ST, "{\n", true, true);
ST = s->getLocEnd();
ST = ST.getLocWithOffset(1);
Rewrite.InsertText(ST, "\n}\n", true, true);
}
return true;
}
This works fine for almost all of the cases. However, in the case when the statement is a MACRO expansion
else
WARN((stderr,"%s: %s is a directory -- ignored\n", progname, ifname));
I get an assertion failure
Assertion `0 && "Invalid SLocOffset or bad function choice"' failed.
Can anyone explain why is this happening? Is there some special case for handling macros in clang?
My Code is below.Whenever I try to run it I get an error saying I'm using duplicate hotkeys when in practice they would never interfere with each other. How do I get around this?
"Your post does not have much context to explain the code sections; please explain your scenario more clearly.". So I guess need to explain my code.. It's extremely simple I have a state variable that is changed by pressing the arrow keys, then I have if statements that checks what the state is. If I press numpad1 when state = "up" the script should type "A", if the state = "right" it would print I. However I'm getting an error since I used the same hotkey multiple times in my different if statements.
state := "none"
UP::
state := "up"
Right::
state := "right"
DOWN::
state := "down"
LEFT::
state := "left"
if (state = "up"){
Numpad1::
Send A
Return
Numpad2::
Send B
Return
Numpad3::
Send C
Return
Numpad4::
Send D
Return
Numpad6::
Send E
Return
Numpad7::
Send F
Return
Numpad8::
Send G
Return
Numpad9::
Send H
Return
}
if (sate = "right"){
Numpad1::
Send I
Return
Numpad2::
Send J
Return
Numpad3::
Send K
Return
Numpad4::
Send L
Return
Numpad6::
Send M
Return
Numpad7::
Send N
Return
Numpad8::
Send O
Return
Numpad9::
Send P
Return
}
if (state = "down"){
Numpad1::
Send Q
Return
Numpad2::
Send R
Return
Numpad3::
Send S
Return
Numpad4::
Send T
Return
Numpad6::
Send U
Return
Numpad7::
Send V
Return
Numpad8::
Send W
Return
Numpad9::
Send X
Return
}
if (state = "left"){
Numpad1::
Send Y
Return
Numpad2::
Send Z
Return
}
When you use AHK_L, you can use #IF with multiple definitions of the same hotkey. WARNING this does NOT work with the regular AHK version.
Here is an example.
You set the variables by typing none\ right\ or left\ .
Depending on the variable setting your Tab key will either send NORMAL, LEFT or RIGHT.
#SingleInstance Force
Tab::Send, NORMAL
#if (state = "left")
Tab::Send, LEFT
#if ; End #if (state = "left")
#if (state = "right")
Tab::Send, RIGHT
#if ; End #if (state = "right")
:*:right\::
state := "right"
Return
:*:left\::
state := "left"
Return
:*:none\::
state := "none"
Return
Alternatively, with the normal AHK, you define ONE hotkey and place IF statements inside the hotkeys to change the behaviour based on the state variable.