Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds GetArg and GetWord methods to Buffer #3112

Merged
merged 1 commit into from
Mar 12, 2024

Conversation

taconi
Copy link
Contributor

@taconi taconi commented Jan 6, 2024

Turns the GetArg function into a buffer object method to be used by lua plugins.

Example:

function init()
  local config = import('micro/config')

  config.MakeCommand('ex', function(bp, args) end, function(buf)
    local cmds = { '-h', '--help' }

    local input, argstart = buf:GetArg()

    local completions = {}
    local suggestions = {}

    for _, a in pairs(cmds) do
      local i, j = a:find(input, 1, true)
      if i == 1 then
        table.insert(suggestions, a)
        table.insert(completions, a:sub(j + 1))
      end
    end

    table.sort(completions)
    table.sort(suggestions)

    return completions, suggestions
  end)
end

Closes #3111

@dustdfg
Copy link
Contributor

dustdfg commented Jan 7, 2024

Idk it seems to me other functions in the buffer/autocomplete.go file looks like utility functions for internal usage of the micro which aren't intended to be used by the buffer itself so they aren't just functions and not methods of buffer

Is it really not imported? Idk where it is done but maybe it would more convenient to export it to lua (if it is still not exported).

What do you think? @dmaluka @JoeKar

@taconi
Copy link
Contributor Author

taconi commented Jan 7, 2024

I can add it to the buffer module to be used with import('micro/buffer') like that:

local buffer = import('micro/buffer')

function complete(buf)
  local input, argstart = buffer.GetArg(buf)
end

The main idea would be to make the GetArg function available so that lua plugins can build their own complete commands.

@taconi taconi changed the title feat: transforms the GetArg function into a buffer object method feat: adds buffer.GetArg for use by lua plugins Jan 7, 2024
@taconi
Copy link
Contributor Author

taconi commented Jan 7, 2024

@dustdfg I added GetArg to the micro/buffer import

@JoeKar
Copy link
Collaborator

JoeKar commented Jan 7, 2024

Exporting GetArg() is most probably the most straight proposal. 👍

@dmaluka
Copy link
Collaborator

dmaluka commented Jan 8, 2024

I would actually prefer the first version, i.e. making GetArg a buffer object method, to avoid unnecessary additions to the "primary" documentation in plugins.md (to keep it small).

Also, if export GetArg, why not export GetWord as well?

@taconi
Copy link
Contributor Author

taconi commented Jan 8, 2024

@dmaluka
I also prefer the first for the same reason, in addition to avoiding an import and for me it makes more sense to buf:GetArg() than buffer.GetArg(buf), but whatever the majority decides I support.

I also thought about exporting GetWord, it would be a good idea, but I preferred to leave the changes minimal for what I needed. Maybe this is the right occasion for this, do I add GetWord to the pull request?

@taconi
Copy link
Contributor Author

taconi commented Jan 14, 2024

I also exported GetWord so it could be used to build plugins

@taconi taconi changed the title feat: adds buffer.GetArg for use by lua plugins feat: exporting buffer utility GetArg and GetWord Jan 14, 2024
@dmaluka
Copy link
Collaborator

dmaluka commented Jan 14, 2024

My vote is still for exporting GetArg and GetWord as buffer methods...

@taconi
Copy link
Contributor Author

taconi commented Jan 14, 2024

Me too, @dustdfg and @JoeKar, what do you think?

@dustdfg
Copy link
Contributor

dustdfg commented Jan 14, 2024

Me too, @dustdfg and @JoeKar, what do you think?

Ok maybe it is better. The functions are used only by buffer itself and is pretended to be used by Compliters that get it as parameter so maybe.... Idk it feels a bit unnatural for me but does it really matter so much? Anyway I think it will be merged by @zyedidia (if it will be) so the last word of which style should be used doesn't belong to none of us

@JoeKar
Copy link
Collaborator

JoeKar commented Jan 14, 2024

Hm, most probably I wasn't attentive enough in the moment I suggested to just export GetArg() (and later GetWord() highlighted by @dmaluka), because @dmaluka has his finger already in the wound:

buffer.GetArg(b) used from package buffer using a pointer to Buffer as argument, which indeed looks strange in the moment directly compared to be a method of the same itself (b.GetArg()) as already started with 607922b.

So @taconi I've to excuse, changed my mind and support your first and @dmaluka approach.
Sorry that it took that long to respond and for the confusion.

Hopefully we will get to the point at which these changes don't rot at private branches/PRs any longer.

@taconi
Copy link
Contributor Author

taconi commented Jan 14, 2024

[...] Idk it feels a bit unnatural for me but does it really matter so much?

For me, the buffer itself should know how to fetch this information. It makes more semantic sense to ask the buffer to get the last word over the cursor (b.GetArg()) than to ask a third party (buffer.GetArg(b)).
I believe this is another unnecessary step towards creating completers.

@taconi taconi changed the title feat: exporting buffer utility GetArg and GetWord feat: adds GetArg and GetWord methods to Buffer Jan 14, 2024
@taconi
Copy link
Contributor Author

taconi commented Jan 14, 2024

I returned the PR to use via buf:GetArg(), buf:GetWord()

@JoeKar JoeKar merged commit fe4ade7 into zyedidia:master Mar 12, 2024
3 checks passed
@taconi taconi deleted the feat/3111 branch March 12, 2024 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Question] How to write a complete for a command
4 participants