Skip to content

Commit

Permalink
Merge pull request #33 from barreiroleo/new_config
Browse files Browse the repository at this point in the history
feat: ltex_extra now can call LTeX server
  • Loading branch information
barreiroleo authored Apr 17, 2023
2 parents 1d2f288 + a29b62e commit 9189152
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 150 deletions.
123 changes: 79 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# LTeX_extra.nvim
<h6>Provides external LTeX file handling (off-spec lsp) and other functions.</h6>
<h6>🚧 This plugin is on development, expect some changes</h6>
<h6>Developed on Nvim stable v0.8</h6>
<h6>Developed on Nvim v0.9, tested on v0.10</h6>


[![Lua](https://img.shields.io/badge/Lua-blue.svg?style=for-the-badge&logo=lua)](http://www.lua.org)
Expand All @@ -13,7 +13,8 @@
<!-- [![Neovim Nightly](https://img.shields.io/badge/Neovim%20Nightly-green.svg?style=for-the-badge&logo=neovim)](https://neovim.io) -->
</div>

`LTeX_extra` is a plugin for Neovim that provide the functions that are called on LSP code actions by `ltex-ls`: [`addToDictionary`](https://valentjn.github.io/ltex/ltex-ls/server-usage.html#_ltexhidefalsepositives-client),
`LTeX_extra` is a plugin for Neovim that provide the functions that are called on LSP code actions by `ltex-ls`:
[`addToDictionary`](https://valentjn.github.io/ltex/ltex-ls/server-usage.html#_ltexhidefalsepositives-client),
[`disableRule`](https://valentjn.github.io/ltex/ltex-ls/server-usage.html#_ltexdisablerules-client),
[`hideFalsePositive`](https://valentjn.github.io/ltex/ltex-ls/server-usage.html#_ltexaddtodictionary-client).
Also, `LTeX_extra` provide extra [features](#features).
Expand Down Expand Up @@ -43,13 +44,6 @@ Check the docs: [neovim suggested configuration](https://github.com/neovim/nvim-

https://user-images.githubusercontent.com/48270301/177694689-b6b12b4a-3981-47fe-aa88-567697f797bd.mp4

#### Lspsaga
Some users reported an issue with code actions when called from lspsaga. I'm not using lspsaga, so PR are very welcome.

https://user-images.githubusercontent.com/39244876/201530888-077e76ad-211c-408f-80dc-89ba59751532.mov

_Thanks to @felipejoribeiro for the screenrecording_

### Custom export path
Config you path, give you compatibility with official vscode extension.

Expand All @@ -61,58 +55,99 @@ Autoload exported data for required languages.
https://user-images.githubusercontent.com/48270301/177694724-736159ab-c202-4325-ad23-405c76676b79.mp4

### Update on demand
Reload exported data on demand:
Reload exported data on demand: `require("ltex_extra").reload()`

https://user-images.githubusercontent.com/48270301/177694740-bc8bdb4c-0f6b-4f63-98af-54ec23196f27.mp4

## Installation
This plugin requires an instance of `ltex_ls` language server attached in the current buffer.

*Note: [`ltex-ls`](https://github.com/valentjn/ltex-ls) is available by [`nvim-lsp-installer`](https://github.com/williamboman/nvim-lsp-installer).*

### Packer
```lua
use { "barreiroleo/ltex-extra.nvim" }
```
This plugin requires an instance of `ltex_ls` language server available to attach.
*[`ltex-ls`](https://github.com/valentjn/ltex-ls) is available at [`mason.nvim`](https://github.com/williamboman/mason.nvim).*

Install the plugin with your favorite plugin manager using `{"barreiroleo/ltex-extra.nvim"}`.
Then add `require("ltex_extra").setup()` to your config in a proper place.

We suggest to you two ways:
- Call the `setup` from `on_attach` function of your server. Example with
`lspconfig`, minor changes are required for `mason` handler:
```lua
require("lspconfig").ltex.setup {
capabilities = your_capabilities,
on_attach = function(client, bufnr)
-- rest of your on_attach process.
require("ltex_extra").setup { your_opts }
end,
settings = {
ltex = { your settings }
}
}
```
- Use the handler which `ltex_extra` provide to call the server. Example of use with `lazy.nvim`:

```lua
return {
"barreiroleo/ltex_extra.nvim",
ft = { "markdown", "tex" },
dependencies = { "neovim/nvim-lspconfig" },
-- yes, you can use the opts field, just I'm showing the setup explicitly
config = function()
require("ltex_extra").setup {
your_ltex_extra_opts,
server_opts = {
capabilities = your_capabilities,
on_attach = function(client, bufnr)
-- your on_attach process
end,
settings = {
ltex = { your settings }
}
},
}
end
}
```

## Configuration
The recommended way to use `ltex-extra` is calling it from `ltex-ls` `on_attach` event.
Next example use `nvim-config` typical launch server and default settings for `ltex-extra`, you can use it as template.

Here are the settings available on `ltex_extra`. You don't need explicit define each
one, just modify what you need.

*Notes: You can pass to set up only the arguments that you are interested in.
At the moment, if you define stuff in `dictionary`, `disabledRules` and `hiddenFalsePositives` in your `ltex` settings, they haven't backup.*
At the moment, if you define stuff in `dictionary`, `disabledRules` and
`hiddenFalsePositives` in your `ltex` settings, they haven't backup.*

```lua
require("lspconfig").ltex.setup {
capabilities = your_capabilities,
on_attach = function(client, bufnr)
-- your other on_attach functions.
require("ltex_extra").setup{
load_langs = { "es-AR", "en-US" }, -- table <string> : languages for witch dictionaries will be loaded
init_check = true, -- boolean : whether to load dictionaries on startup
path = nil, -- string : path to store dictionaries. Relative path uses current working directory
log_level = "none", -- string : "none", "trace", "debug", "info", "warn", "error", "fatal"
}
end,
settings = {
ltex = {
-- your settings.
}
}
require("ltex_extra").setup {
-- table <string> : languages for witch dictionaries will be loaded, e.g. { "es-AR", "en-US" }
-- https://valentjn.github.io/ltex/supported-languages.html#natural-languages
load_langs = {}, -- en-US as default
-- boolean : whether to load dictionaries on startup
init_check = true,
-- string : relative or absolute paths to store dictionaries
-- e.g. subfolder in current working directory: ".ltex"
-- e.g. shared files for all projects : vim.fn.expand("~") .. "/.local/share/ltex"
path = "", -- current working directory
-- string : "none", "trace", "debug", "info", "warn", "error", "fatal"
log_level = "none",
-- table : configurations of the ltex language server.
-- Only if you are calling the server from ltex_extra
server_opts = nil
}
```

### Contributors
## Contributors

Thanks to these people for your time, effort and ideas.

<a href="https://github.com/barreiroleo/ltex_extra.nvim/graphs/contributors">
<img src="https://contrib.rocks/image?repo=barreiroleo/ltex_extra.nvim" />
</a>

## To-do list
- [ ] Write the docs.
- [x] Add capability for create dictionary, disabledRules and hiddenFalsePositives keys in LTeX settings.
- [x] Add path specification for files in setup.
- [x] Add capability for read the existing files in path.
- [ ] Abort initial load if the files doesn't exist.
## Issues
- Lspsaga:

Some users reported an issue with code actions when called from lspsaga.
I'm not using lspsaga, so PR are very welcome.

https://user-images.githubusercontent.com/39244876/201530888-077e76ad-211c-408f-80dc-89ba59751532.mov

_Thanks to @felipejoribeiro for the screenrecording_
File renamed without changes.
39 changes: 39 additions & 0 deletions docs/diagrams/Init_flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Initial flow

Generated with [Diagon](https://arthursonzogni.com/Diagon/#Flowchart)

```
"START";
if("Should be load ltex?"){
if("Ltex is running?"){
"Load ltex from lspconfig";
}
else{
"Alert ltex already running";
}
}
else{
"Load plugins opts and attach to a running ltex instance"
}
"END";
```

```
┌─────┐
│START│
└──┬──┘
_________▽__________ ________________ ┌──────────────┐
╱ ╲ ╱ ╲ │Load ltex from│
╱ Should be load ltex? ╲___╱ Ltex is running? ╲___│lspconfig │
╲ ╱yes╲ ╱yes└───────┬──────┘
╲____________________╱ ╲________________╱ │
│no │no │
┌──────────────▽─────────────┐ ┌───────▽───────┐ │
│Load plugins opts and attach│ │Alert ltex │ │
│to a running ltex instance │ │already running│ │
└──────────────┬─────────────┘ └───────┬───────┘ │
└────────────┬───────────┴─────────────────────┘
┌─▽─┐
│END│
└───┘
```
52 changes: 52 additions & 0 deletions docs/diagrams/Init_sequence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Sequence
`cat architecture.md | plantuml_asci`

@startuml
nvim -> ltex_extra : load
ltex_extra -> nvim : is ltex running?
nvim -> ltex_extra : ltex status
ltex_extra -> ltex : start
@enduml

┌────┐ ┌──────────┐ ┌────┐
│nvim│ │ltex_extra│ │ltex│
└─┬──┘ └────┬─────┘ └─┬──┘
│ load │ │
│ ─────────────────> │
│ │ │
│ is ltex running? │ │
│ <───────────────── │
│ │ │
│ ltex status │ │
│ ─────────────────> │
│ │ │
│ │ start │
│ │ ─────────────────>
┌─┴──┐ ┌────┴─────┐ ┌─┴──┐
│nvim│ │ltex_extra│ │ltex│
└────┘ └──────────┘ └────┘

@startuml
nvim -> ltex_extra : load
ltex_extra -> nvim : is ltex running?
nvim -> ltex_extra : ltex status
ltex_extra -> ltex : ltex is already running
@enduml

┌────┐ ┌──────────┐ ┌────┐
│nvim│ │ltex_extra│ │ltex│
└─┬──┘ └────┬─────┘ └─┬──┘
│ load │ │
│ ─────────────────> │
│ │ │
│ is ltex running? │ │
│ <───────────────── │
│ │ │
│ ltex status │ │
│ ─────────────────> │
│ │ │
│ │ ltex is already running│
│ │ ───────────────────────>
┌─┴──┐ ┌────┴─────┐ ┌─┴──┐
│nvim│ │ltex_extra│ │ltex│
└────┘ └──────────┘ └────┘
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
local log = require("ltex_extra.src.log")
local log = require("ltex_extra.utils.log")

local exportFile = require("ltex_extra.src.utils").exportFile
local loadFile = require("ltex_extra.src.utils").loadFile
local exportFile = require("ltex_extra.utils.fs").exportFile
local loadFile = require("ltex_extra.utils.fs").loadFile

local types = {
["dict"] = "dictionary",
["dRules"] = "disabledRules",
["hRules"] = "hiddenFalsePositives"
["hRules"] = "hiddenFalsePositives",
}

local function catch_ltex()
log.trace("catch_ltex")
local buf_clients = vim.lsp.buf_get_clients()
local client = nil
for _, lsp in pairs(buf_clients) do
if lsp.name == "ltex" then client = lsp end
end
return client
end

local function get_settings(client)
if not client.config.settings.ltex then
client.config.settings.ltex = {}
Expand All @@ -36,30 +26,39 @@ local function update_dictionary(client, lang)
local settings = get_settings(client)
settings.ltex.dictionary[lang] = loadFile(types.dict, lang)
log.debug(vim.inspect(settings.ltex.dictionary))
return client.notify('workspace/didChangeConfiguration', settings)
return client.notify("workspace/didChangeConfiguration", settings)
end

local function update_disabledRules(client, lang)
log.trace("update_disabledRules")
local settings = get_settings(client)
settings.ltex.disabledRules[lang] = loadFile(types.dRules, lang)
log.debug(vim.inspect(settings.ltex.disabledRules))
return client.notify('workspace/didChangeConfiguration', settings)
return client.notify("workspace/didChangeConfiguration", settings)
end

local function update_hiddenFalsePositive(client, lang)
log.trace("update_hiddenFalsePositive")
local settings = get_settings(client)
settings.ltex.hiddenFalsePositives[lang] = loadFile(types.hRules, lang)
log.debug(vim.inspect(settings.ltex.hiddenFalsePositives))
return client.notify('workspace/didChangeConfiguration', settings)
return client.notify("workspace/didChangeConfiguration", settings)
end

local M = {}

M.updateConfig = function(configtype, lang)
function M.catch_ltex()
log.trace("catch_ltex")
local buf_clients = vim.lsp.get_active_clients({
bufnr = vim.api.nvim_get_current_buf(),
name = "ltex",
})
return buf_clients[1]
end

function M.updateConfig(configtype, lang)
log.trace("updateConfig")
local client = catch_ltex()
local client = M.catch_ltex()
if client then
if configtype == types.dict then
update_dictionary(client, lang)
Expand All @@ -72,12 +71,11 @@ M.updateConfig = function(configtype, lang)
return vim.notify("Config type unknown")
end
else
log.fmt_error("Error catching ltex client")
return vim.notify("Error catching ltex client")
return error("Error catching ltex client",1)
end
end

M.reload = function(langs)
function M.reload(langs)
log.trace("updateConfigFull")
langs = langs or package.loaded.ltex_extra.opts.load_langs
for _, lang in pairs(langs) do
Expand All @@ -90,7 +88,7 @@ M.reload = function(langs)
end
end

M.addToDictionary = function(command)
function M.addToDictionary(command)
log.trace("addToDictionary")
local args = command.arguments[1].words
for lang, words in pairs(args) do
Expand All @@ -102,7 +100,7 @@ M.addToDictionary = function(command)
end
end

M.disableRules = function(command)
function M.disableRules(command)
log.trace("disableRules")
local args = command.arguments[1].ruleIds
for lang, rules in pairs(args) do
Expand All @@ -114,7 +112,7 @@ M.disableRules = function(command)
end
end

M.hideFalsePositives = function(command)
function M.hideFalsePositives(command)
log.trace("hideFalsePositives")
local args = command.arguments[1].falsePositives
for lang, rules in pairs(args) do
Expand Down
Loading

0 comments on commit 9189152

Please sign in to comment.