jalvesaq / Nvim-R

Vim plugin to work with R
GNU General Public License v2.0
968 stars 125 forks source link

The completion candidates order is not the parameters true order #815

Closed shaman-yellow closed 5 months ago

shaman-yellow commented 5 months ago

I have always had a question: Nvim-R uses cmp to provide function parameter completion, but in many cases the order of the parameters is not followed. Let me give you an example and create the following function:

sortCh <- function(x, decreasing = F, as.factor = F) {
  num <- as.integer(stringr::str_extract(x, "[0-9]+"))
  x <- x[ order(num, decreasing = decreasing) ]
  if ( as.factor ) {
    x <- factor(x, levels = unique(x))
  }
  x
}

The true order is: x, decreasing = F, as.factor = F. However, the completion candidates order is: x, as.factor = F, decreasing = F. Many times, I am deceived by the completion. Is there any way to adjust this?

jalvesaq commented 5 months ago

nvim-cmp receives the arguments in the correct order from cmp-nvim-r. You could try to configure the sorting or sorting.comparators argument in nvim-cmp config so that at least the order of items from cmp-nvim-r is preserved. Please, post here if you have success doing it.

shaman-yellow commented 5 months ago

Thanks! After trying, I solved the trouble that had been bothering me for a long time!

cmp.setup({
  -- ...
  -- before is the other configure
  sources = {
    { name = 'cmp_nvim_r' },
  },
  sorting = {
    comparators = {
      -- this function seems to work well
      cmp.config.compare.order, 
    },
  },
})
jalvesaq commented 5 months ago

Thank you! I will try your solution too.