Git Product home page Git Product logo

Comments (7)

dzfrias avatar dzfrias commented on June 13, 2024 1

Project-specific tasks can be implemented pretty easily with a .vscode folder in your project root. Overseer will read these tasks and add them to your task selector. This can be done in lua, but I wouldn't recommend it, as it pollutes your dotfiles with task templates that only work for specific projects.

However, if you want to use lua, see the callback field of condition, which can execute a function to determine whether a task should be visible in the task runner.

A project-specific lua task can be implemented like this:

{
  name = 'Compile',
  builder = function()
    local file = vim.fn.expand '%:p'
    return {
      cmd = { 'g++' },
      args = { file },
      name = 'Compile ' .. file,
      components = { 'default' },
    }
  end,
  condition = {
    filetype = { 'cpp' },
    -- Only show if this function returns true
    callback = function()
      -- Assume project root is the directory containing '.git'
      local git_path = vim.fn.finddir('.git', vim.fn.getcwd() .. ';')
      local proj_path = vim.fn.fnamemodify(git_path, ':h')
      -- Make sure that the project path ends in a '/'
      if vim.fn.fnamemodify(proj_path, ':p') == 'path/to/project/' then
        return true
      end
      return false
    end,
  },
}

from overseer.nvim.

stevearc avatar stevearc commented on June 13, 2024 1

Damn, @dzfrias again you're so fast!

I would generally agree that for project-specific tasks .vscode/tasks.json is the way to go. It's a data-only format so it's safer than running arbitrary code in your editor, and it's not neovim-specific, so people with other editors can benefit from it.

If you want to do it with lua, you definitely can. If you want to load and run files that are local to your project, you'll have to pick some other plugin to do that or roll your own. Overseer does not have a system for auto-loading lua files in a directory, and I don't plan to add one.

Referencing the docs on custom tasks, you can dynamically register a task with overseer.register_template. The example above is great, but I would make one slight tweak: there is a dir condition that's easier to use than the callback.

require('overseer').register_template({
  name = 'Compile',
  builder = function()
    local file = vim.fn.expand '%:p'
    return {
      cmd = { 'g++' },
      args = { file },
      name = 'Compile ' .. file,
      components = { 'default' },
    }
  end,
  condition = {
    filetype = { 'cpp' },
    dir = "/path/to/project",
  },
})

from overseer.nvim.

dzfrias avatar dzfrias commented on June 13, 2024

The builder function is called when the task is selected for running, so yes, you could get the name of the file in the current buffer. A lot of good examples of task templates and other things in the Overseer ecosystem are in the source code. This link has a bunch of built-ins that you should check out if you want to write your own.

Here is an example of a task template to compile a cpp file:

{
  -- Name in the task selector
  name = 'Compile',
  builder = function()
    -- Get file's absolute path
    local file = vim.fn.expand '%:p'
    return {
      cmd = { 'g++' },
      args = { file },
      -- Name in the task list
      name = 'Compile ' .. file,
      components = { 'default' },
    }
  end,
  condition = {
    -- Only show this task if the filetype is cpp
    filetype = { 'cpp' },
  },
}

Feel free to ask any questions about this code.

You can get vscode variables pretty easily, using lua (for example, "${file}" for vscode is equivalent to vim.fn.expand("%:p") in lua, as shown in the example above). See the source for their actual implementation in lua.

from overseer.nvim.

stevearc avatar stevearc commented on June 13, 2024

Thanks @dzfrias for jumping in here! That is an excellent explanation and example, I couldn't have said it better.

@dcordb based on your question (and others I've received), I've decided to overhaul the documentation. I have a work-in-progress PR at #29, and you can browse the new docs by going to https://github.com/stevearc/overseer.nvim/tree/stevearc-doc.

I've taken the example here and added it to the new tutorials doc, along with some others. Please feel free to leave feedback on the PR! There's a few more things I have to get working (mostly API doc generation) before I can merge it.

from overseer.nvim.

dcordb avatar dcordb commented on June 13, 2024

This is really good! Great editorial, I could configure it without a problem. To add something to what @dzfrias wrote that might be useful for others, I also created a mapping to build the cpp file.

I created a cpp.lua in after/ftplugin directory, the file looks like this:

local overseer = require("overseer")

vim.keymap.set("n",
  "<M-b>",
  function() overseer.run_template({ name = "g++ build" }) end,
  { buffer = true }
)

This way the mapping only appears in buffers with cpp filetype.

from overseer.nvim.

dcordb avatar dcordb commented on June 13, 2024

I have other questions that may go in a different issue:

  • What about project specific tasks?
  • Do vscode tasks work as project specific tasks?
  • Can this be done also in Lua? (although this would mean that the plugin needs to read Lua files from some folder, this might be insecure).

from overseer.nvim.

dcordb avatar dcordb commented on June 13, 2024

I see the recipes are already on master. The issue is resolved, so I'm closing this.

from overseer.nvim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.