Skip to content

spywhere/detect-language.nvim

Repository files navigation

detect-language.nvim

This plugin still under development

This plugin provide a better language auto-detection to neovim, powered by tree-sitter

demo

Installation

Install using a plugin manager of your choice, for example

Plug 'nvim-treesitter/nvim-treesitter'   " not required but recommended
Plug 'spywhere/detect-language.nvim'

Setup

Simply put the following configuration to your .lua config file

require('detect-language').setup {}

The following configuration is defaults included with this plugin

local detect_language = require('detect-language')
detect_language.setup {
  -- list of languages to be auto-detected (must be supported by tree-sitter)
  languages = {
    'javascript',
    'typescript',
    'tsx',
    'bash',
    'c_sharp',
    'cpp',
    'c',
    'go',
    'graphql',
    'html',
    'java',
    'json5',
    'jsonc',
    'json',
    'lua',
    'php',
    'python',
    'rust',
    'scala',
    'scss',
    'toml',
    'vim',
    'yaml'
  },
  -- auto-detection analyser (see Analyser section below for options)
  provider = detect_language.provider.treesitter { minimum = 0 },
  -- language picker (see Picker section below for options)
  picker = detect_language.picker.sensible { top = 3 },
  -- autocmd events to trigger auto-detection
  events = { 'InsertLeave', 'TextChanged', 'FileReadPost' },
  -- command configurations
  commands = {
    -- Prefix for command (set to empty will disable all commands)
    prefix = 'DetectLanguage',
    -- Enable buffer toggle command (suffixed with 'BufToggle')
    toggle = true,
    -- Enable buffer enable command (suffixed with 'BufEnable')
    enable = true,
    -- Enable buffer disable command (suffixed with 'BufDisable')
    disable = true,
    -- Enable manual trigger for auto-detection command (no suffix)
    oneshot = true,
    -- Enable command for listing language scores (suffixed with 'BufScore')
    score_list = false
  },
  -- disable auto-detection for buffer over this number of lines (set to 0 for no limit)
  max_lines = 100,
  -- fine-grain setup
  disable = {
    -- disable auto-detection on new buffer
    new = false,
    -- disable auto-detection on buffer with no extension
    no_extension = true
  }
}

Languages listed in the configuration are order sensitive, to allow prioritization of language ordering. So if JavaScript comes before TypeScript and both has the same score, JavaScript will be picked.

Analyser

Treesitter

Language analyser powered by tree-sitter

require('detect-language').provider.treesitter {
  -- minimum score to be considered as candidate languages
  minimum = 0
}

Picker

Sensible

A sensible language picker using the algorithm explained below

require('detect-language').picker.sensible {
  -- allow up to this number of the same score, otherwise will not pick
  top = 3
}

Algorithm

  • Pick the language with highest score
  • Keep track of languages with the same highest score
  • Pick the first language if there are less than or equal to n number of languages with highest score
  • Otherwise, nothing is pick

How it works

This plugin simply list all available languages supported by the specific provider, then it will iterate through each of the language. The whole document will pass to its provider's analyser and keep record of the score produced. Once all the languages are paresed, all scores will be submitted to the picker to select the suitable language.

About

neovim plugin for better language auto-detection, powered by tree-sitter

Topics

Resources

Stars

Watchers

Forks

Sponsor this project