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(config): specify filetype in filetype_details #43

Open
AgatZan opened this issue Sep 26, 2024 · 3 comments
Open

feat(config): specify filetype in filetype_details #43

AgatZan opened this issue Sep 26, 2024 · 3 comments

Comments

@AgatZan
Copy link
Contributor

AgatZan commented Sep 26, 2024

after creating file do :set filetype=filetype_details[choice] and ...

Draft

-- config
{
   -- ...
   manual_selector_text = "MANUAL_INPUT",
   -- If it really matter to make default choice, not that hard to generate some defaults for win_conf
  --[[
    window-relative float
          {relative='win', row=3, col=3, width=12, height=3}

    vertical split left of the current window
          {split = 'left', win = 0}
  ]]
   win_conf = {} ---@see: `nvim_open_win`,
   select_options = {"lua", "lua-prof", "foo-bar"},
   filetype_details = {
      ["lua-prof"] = {
          filetype = "lua",
          content = {"strings..."},
          cursor = { location = {0, 5}, insert = true },
          ---@type fun(string):string
          filename_generator = function(scratch_path) end, --gen new name with folders if needed
          win_conf = {} ---@see: `nvim_open_win`
      }
   }
}

Then, the function that creates a scratch buffer can appear in this way.

function M.create_scratch(scratch_file_dir, ft, filetype_details)
  local opts = filetype_details[ft]
  local abs_path = (opts.filename_generator or function(base_dir)
    return base_dir .. os.date("%y-%m-%d_%H-%M-%S") .. "." .. ft
  end)(scratch_file_dir)

  local buf = vim.api.nvim_create_buf(true, false)
  vim.api.nvim_buf_set_name(buf, abs_path)
  local filetype = opts.filetype or vim.filetype.match({ filename = abs_path })
  vim.api.nvim_set_option_value("filetype", filetype, { buf = buf })
  
  local suc, msg = pcall(vim.api.nvim_open_win, buf, true, opts.win_config)
  if not opts.win_config then
    vim.api.nvim_set_current_buf(buf)
  elseif not suc then
    vim.notify(msg, vim.log.levels.WARN)
    vim.api.nvim_set_current_buf(buf)
  end
  if opts.content then
    vim.api.nvim_buf_set_lines(buf, 0, -1, false, opts.content)
  end
  if opts.cursor then
    vim.api.nvim_win_set_cursor(0, opts.cursor.location)
    if opts.cursor.insert_mode then
      vim.api.nvim_feedkeys("a", "n", true)
    end
  end
end 

Benefit

Replace chore: requireDir, filename and win_cwd, and give more power to specify what file type you need and how it should look.

Examples

If you make notes, then you may want to create scratch note, you know where them live, so you can

  filetypes = {..., "note"},
  filetype_details = {
    ...
    note = {
       filetype = "your_note_filetype",
       filename_generator = function(_)
          return NOTE_DIR .. os.time() .. ".ynf"
       end
    }
  }
@LintaoAmons
Copy link
Owner

I see the value of this filename_generator and would love to have it inside the filetype_detail entity.

But I think it quite a powerful user stuff, and would still remind simple config options like require_dir, filenamewin_cwd

I consider it as a more powerful counterpart of filename option and would like it to have higher priority to user filename_generator over filename if both of them are existing inside the config of a filetype_detail entity

@AgatZan
Copy link
Contributor Author

AgatZan commented Oct 8, 2024

By the way, I guess require_dir, filename, and win_cwd can all be implemented as defaults. Something like require("scratch.default.win_cmd").split_left or require("scratch.default.file_generator").with_require_dir

Also, in this case, I don't want to sound rude, but the default way of specifying default content is not user friendly enough.

I think not that hard to make it looks something like that

filetype_details = {
  lua = {
    content ={ 
      text = [[default content blah blah blah
new line ${cursor} fdsjjdf
asdfjlksdfjl]], 
      insert=true
    }
  }
}

@LintaoAmons
Copy link
Owner

No worry, yeah, it’s better to make it a multi line string

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

No branches or pull requests

2 participants