Skip to content

Commit

Permalink
feat: provide config value to control fallback behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jcha0713 committed Sep 23, 2022
1 parent caba0c4 commit e1a29d9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
22 changes: 21 additions & 1 deletion README-KO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Neovim에 내장된 treesitter 라이브러리를 이용하여 파일 내의 css

`cmp-tw2css``nvim-cmp` 엔진을 통해 코드를 제공합니다. 플러그인 사용을 위해서는 `nvim-cmp`가 neovim에 설치된 상태여야 합니다. `nvim-cmp`를 설치하는 방법은 [nvim-cmp Github repo](https://github.com/hrsh7th/nvim-cmp)를 참고해주세요.

`cmp-tw2css`는 버전 1.0.0부터 css 파서(parser)를 이용하여 커서가 css 코드 블록 안에 있는지 확인합니다. 따라서 이 기능을 이용하기 위해서는 css 파서를 필요로 합니다. 더 자세한 내용은 [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter)에서 찾을 수 있습니다.

### 설치 방법

Expand Down Expand Up @@ -72,6 +73,25 @@ body {
</html>
```

## 변수 설정

```lua
require('cmp-tw2css').setup({ ... })
```

### `fallback`

**type**: `boolean`
**default**: `true`

```lua
{
fallback = true
}
```

treesitter 파서가 없을 때 자동 완성 소스를 로드할지 말지 정합니다. `true`로 설정하는 경우에는 css 파서 없이도 소스를 로드하지만 커서 위치에 관계없이 모든 상황에서 소스를 불러오게 됩니다. `false`로 설정하면 css 파서가 없는 상황에서는 완성 소스를 불러오지 않습니다.

## 사용 전에 알아야 할 주의 사항

`cmp-tw2css`는 tailwindcss 공식 문서를 스크래핑하여 만든 파일을 통해 완성 소스를 제공합니다. 따라서 공식 문서에 내용이 추가되거나 제외되는 부분을 바로 반영하기가 힘듭니다. 또한 미리 만들어진 파일을 통해 제공되는 소스이기 때문에 사용자가 원하는대로 값을 수정하는 게 불가능합니다. Tailwindcss 문서가 제공하는 내용과 다른 부분을 찾게 된다면 언제든지 issue를 생성해주세요.
Expand All @@ -81,7 +101,7 @@ body {
## 추가될 기능들

- [x] 커서가 css 코드 블록 안에 있을 때만 소스를 로드하기
- [ ] treesitter 기능을 사용자가 제어할 수 있게 하기
- [x] treesitter 기능을 사용자가 제어할 수 있게 하기
- [ ] 완성 소스를 고를 때 documentation 보여주기
- [ ] LSP를 이용하여 웹 스크래핑 없이 소스 제공하기

Expand Down
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ It uses treesitter to find out whether any css code block exists in the code and

`cmp-tw2css` uses `nvim-cmp` to provide the code. You first need to have `nvim-cmp` installed in your neovim. To install `nvim-cmp`, please visit the [nvim-cmp Github repo](https://github.com/hrsh7th/nvim-cmp).

Since version 1.0.0, `cmp-tw2css` uses css parser to detect if the cursor is inside the css code block. To fully use this feature, you need to install the css parser through `nvim-treesitter`. For more information, please refer to [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter).

### Installation

To install `cmp-tw2css`, I recommend using [`packer.nvim`](https://github.com/wbthomason/packer.nvim).
Expand Down Expand Up @@ -73,16 +75,35 @@ body {
</html>
```

## Configuration

```lua
require('cmp-tw2css').setup({ ... })
```

### `fallback`

**type**: `boolean`
**default**: `true`

```lua
{
fallback = true
}
```

Determines whether to load the completion items when there is no treesitter parser. If this is set to `true`, you are allowing `cmp-tw2css` to load the completion items regardless of your cursor position. If it is set to `false`, then it simply does not load anything when there is no css parser.

## Limitation

There are a number of limitations to `cmp-tw2css`. First, the source of this plugin is a result of web scraping. This means that you might find some items are missing while using. If this happens to you, please let me know by submitting an issue so that I can update the source accordingly. Another downside is that it can't be dynamically generated and only provides the code from the official website.
There are a number of limitations to `cmp-tw2css`. First, the source of this plugin is a result of web scraping. This means that you might find some items are missing while using. If this happens to you, please let me know by submitting an issue so that I can update the source accordingly. Another downside is that it can't be dynamically generated and only provides the code from the official website.

Currently `cmp-tw2css` does not automatically add tabs for the additional lines when the insert text is more than one line. And also when there are more than one colon(`:`) in a line, `cmp-tw2css` cannot properly load its completion source.

## Roadmap

- [x] Load the source only when the cursor is inside the code block
- [ ] Provide ways to configure with treesitter
- [x] Provide ways to configure with treesitter
- [ ] Show documentation when selecting an item...?
- [ ] Provide the completion source using LSP functionality

Expand Down
19 changes: 17 additions & 2 deletions lua/cmp-tw2css/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
local source = {}
local opt = {
fallback = true,
}

---@class cmp-tw2css.source
---@field public is_sorted boolean
Expand All @@ -10,6 +13,12 @@ source.new = function()
return self
end

source.setup = function(user_opt)
if user_opt then
opt = vim.tbl_deep_extend("force", opt, user_opt)
end
end

--- Checks if the lang is css or scss
---@param lang string
---@return boolean
Expand Down Expand Up @@ -134,9 +143,13 @@ function source:complete(params, callback)
local buf_lang = get_buf_lang(bufnr)
local ok, root_lang_tree = pcall(vim.treesitter.get_parser, bufnr, buf_lang)

-- no treesitter, no completion
if not ok then
callback()
if opt.fallback then
local items = source:get_sorted_items()
callback(items)
else
callback()
end
return
end

Expand Down Expand Up @@ -179,4 +192,6 @@ function source:complete(params, callback)
callback()
end

-- require("cmp").register_source("cmp-tw2css", source.new())

return source

0 comments on commit e1a29d9

Please sign in to comment.