From 6c326e0fc6c47cb8685204629fb50f53f3aaeefa Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Thu, 2 May 2024 03:23:38 +0300 Subject: [PATCH 1/6] docs: update readme --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5f4306b..4c7616b 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Snippet -- stable version require("lazy").setup({ "chenasraf/text-transform.nvim", - version = "*", + version = "*", -- or: tag = "stable" dependencies = { -- for Telescope popup 'nvim-telescope/telescope.nvim', @@ -75,7 +75,7 @@ require("lazy").setup({ -- dev version require("lazy").setup({ "chenasraf/text-transform.nvim", - tag = "dev", + branch = "develop", dependencies = { -- for Telescope popup 'nvim-telescope/telescope.nvim', @@ -101,7 +101,7 @@ use { "chenasraf/text-transform.nvim", } -- dev version use { "chenasraf/text-transform.nvim", - tag = "dev", + branch = "develop", } ``` @@ -126,7 +126,7 @@ Plug "chenasraf/text-transform.nvim", { } -- dev version Plug "chenasraf/text-transform.nvim", { - "tag": "dev", + "branch": "develop", } ``` From f74c85b6c2d990a2d514d0c5f5e35af3e4f18fb9 Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Thu, 2 May 2024 03:27:47 +0300 Subject: [PATCH 2/6] ci: disable workflow concurrency --- .github/workflows/develop.yml | 6 +++--- .github/workflows/main.yml | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/develop.yml b/.github/workflows/develop.yml index 8eb97a2..fe876b6 100644 --- a/.github/workflows/develop.yml +++ b/.github/workflows/develop.yml @@ -7,9 +7,9 @@ on: branches: [develop] types: [opened, synchronize] -concurrency: - group: github.head_ref - cancel-in-progress: true +# concurrency: +# group: github.head_ref +# cancel-in-progress: true jobs: lint: diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 40180a9..b0d99b4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,9 +7,9 @@ on: branches: [master] types: [opened, synchronize] -concurrency: - group: github.head_ref - cancel-in-progress: true +# concurrency: +# group: github.head_ref +# cancel-in-progress: true jobs: lint: From 88a354e887ecfc87e99510a33fcd96f51148761b Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Thu, 2 May 2024 03:31:54 +0300 Subject: [PATCH 3/6] ci: update workflow version --- .github/workflows/develop.yml | 12 ++++++------ .github/workflows/main.yml | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/develop.yml b/.github/workflows/develop.yml index fe876b6..b35b4e1 100644 --- a/.github/workflows/develop.yml +++ b/.github/workflows/develop.yml @@ -16,9 +16,9 @@ jobs: runs-on: ubuntu-latest name: lint steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - - uses: JohnnyMorganz/stylua-action@v2 + - uses: JohnnyMorganz/stylua-action@v4 with: token: ${{ secrets.GITHUB_TOKEN }} version: latest @@ -28,7 +28,7 @@ jobs: runs-on: ubuntu-latest name: documentation steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 2 @@ -59,12 +59,12 @@ jobs: neovim_version: ['v0.7.2', 'v0.8.3', 'v0.9.0', 'nightly'] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - run: date +%F > todays-date - name: restore cache for today's nightly. - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: _neovim key: ${{ runner.os }}-x64-${{ hashFiles('todays-date') }} @@ -86,7 +86,7 @@ jobs: - tests runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: google-github-actions/release-please-action@v3 id: release diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b0d99b4..1d0811d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,9 +16,9 @@ jobs: runs-on: ubuntu-latest name: lint steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - - uses: JohnnyMorganz/stylua-action@v2 + - uses: JohnnyMorganz/stylua-action@v4 with: token: ${{ secrets.GITHUB_TOKEN }} version: latest @@ -28,7 +28,7 @@ jobs: runs-on: ubuntu-latest name: documentation steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 2 @@ -59,12 +59,12 @@ jobs: neovim_version: ['v0.7.2', 'v0.8.3', 'v0.9.0', 'nightly'] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - run: date +%F > todays-date - name: restore cache for today's nightly. - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: _neovim key: ${{ runner.os }}-x64-${{ hashFiles('todays-date') }} @@ -86,7 +86,7 @@ jobs: - tests runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: google-github-actions/release-please-action@v3 id: release From a16dd8ee7a84fe99305fffcbe2a793b38cacc50c Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Thu, 2 May 2024 03:47:53 +0300 Subject: [PATCH 4/6] ci: update workflows --- .github/workflows/develop.yml | 2 +- .github/workflows/main.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/develop.yml b/.github/workflows/develop.yml index b35b4e1..2bb5688 100644 --- a/.github/workflows/develop.yml +++ b/.github/workflows/develop.yml @@ -88,7 +88,7 @@ jobs: steps: - uses: actions/checkout@v4 - - uses: google-github-actions/release-please-action@v3 + - uses: google-github-actions/release-please-action@v4 id: release with: release-type: simple diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1d0811d..48b8755 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -88,7 +88,7 @@ jobs: steps: - uses: actions/checkout@v4 - - uses: google-github-actions/release-please-action@v3 + - uses: google-github-actions/release-please-action@v4 id: release with: release-type: simple From 981169bf20a7527b1a48d14a9c5e3b8e7b3a5b6e Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Sat, 4 May 2024 01:00:23 +0300 Subject: [PATCH 5/6] docs: update readme --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 4c7616b..03c3d0e 100644 --- a/README.md +++ b/README.md @@ -98,10 +98,20 @@ require("lazy").setup({ -- stable version use { "chenasraf/text-transform.nvim", tag = "stable", + requires = { + -- for Telescope popup + 'nvim-telescope/telescope.nvim', + 'nvim-lua/plenary.nvim', + } } -- dev version use { "chenasraf/text-transform.nvim", branch = "develop", + requires = { + -- for Telescope popup + 'nvim-telescope/telescope.nvim', + 'nvim-lua/plenary.nvim', + } } ``` From 2ac91e1c5b8a9c18f87fd5adbd301603438b766b Mon Sep 17 00:00:00 2001 From: Chen Asraf Date: Sat, 4 May 2024 02:23:51 +0300 Subject: [PATCH 6/6] feat: sort telescope by usage frequency --- lua/text-transform/telescope.lua | 109 +++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 29 deletions(-) diff --git a/lua/text-transform/telescope.lua b/lua/text-transform/telescope.lua index 49a2d9b..d7c7859 100644 --- a/lua/text-transform/telescope.lua +++ b/lua/text-transform/telescope.lua @@ -1,12 +1,15 @@ +local D = require("text-transform.util.debug") local state = require("text-transform.state") local replacers = require("text-transform.replacers") local pickers = require("telescope.pickers") local finders = require("telescope.finders") -local conf = require("telescope.config").values +local telescope_conf = require("telescope.config").values local actions = require("telescope.actions") local action_state = require("telescope.actions.state") local dropdown = require("telescope.themes").get_dropdown({}) +local Sorter = require("telescope.sorters").Sorter +local generic_sorter = telescope_conf.generic_sorter() local TextTransform = {} @@ -20,25 +23,77 @@ local items = { { label = "CONST_CASE", value = "const_case" }, } --- local default_frequency = { --- camel_case = 7, --- snake_case = 6, --- pascal_case = 5, --- kebab_case = 4, --- dot_case = 3, --- title_case = 2, --- const_case = 1, --- } --- --- local frequency_file = vim.fn.stdpath("config") .. "/text-transform-frequency.json" --- local frequency --- if vim.fn.filereadable(frequency_file) == 0 then --- frequency = default_frequency --- vim.fn.writefile({ vim.fn.json_encode(frequency) }, frequency_file) --- else --- frequency = vim.fn.json_decode(vim.fn.readfile(frequency_file)) --- end +local default_frequency = { + camel_case = 1, + snake_case = 1, + pascal_case = 1, + kebab_case = 1, + dot_case = 1, + title_case = 1, + const_case = 1, +} + +local frequency_file = vim.fn.stdpath("config") .. "/text-transform-frequency.json" +local frequency + +local function load_frequency() + if frequency then + return frequency + end + if vim.fn.filereadable(frequency_file) == 0 then + frequency = default_frequency + vim.fn.writefile({ vim.fn.json_encode(frequency) }, frequency_file) + else + frequency = vim.fn.json_decode(vim.fn.readfile(frequency_file)) + end + D.log("telescope", "frequency loaded: %s", vim.inspect(frequency)) + return frequency +end + +local function inc_frequency(name) + frequency[name] = (frequency[name] or 0) + 1 + D.log("telescope", "new frequency: %s %d", name, frequency[name]) + vim.fn.writefile({ vim.fn.json_encode(frequency) }, frequency_file) +end + +local function entry_maker(entry) + return { + value = entry.value, + display = entry.label, + ordinal = entry.label, + frequency = frequency[entry.value] or 1, + } +end + +local frequency_sorter = Sorter:new({ + ---@diagnostic disable-next-line: unused-local + scoring_function = function(self, prompt, line) + local entry + for _, item in ipairs(items) do + if item.label == line then + entry = entry_maker(item) + break + end + end + -- Basic filtering based on prompt matching, non-matching items score below 0 to exclude them + local basic_score = generic_sorter:score(prompt, entry) or 0 + -- D.log("telescope", "basic_score: %s", basic_score) + if basic_score < 0 then + return basic_score + end + + -- D.log("telescope", "entry: %s", vim.inspect(entry)) + -- D.log("telescope", "prompt: %s", prompt) + -- Calculate score based on frequency, higher frequency should have lower score + local freq_score = (entry.frequency or 1) -- Multiply by -1 because we want higher frequency to have lower score + + -- D.log("telescope", "freq_score: %s", freq_score) + D.log("telescope", "%s final_score: %s", line, 99999999 - freq_score * 100 + basic_score) + -- Combine scores, with frequency having the primary influence if present + return 99999999 - freq_score * 100 + basic_score -- Division to ensure frequency has a higher weight + end, +}) ---@diagnostic disable-next-line: unused-local -- for _i, k in pairs(default_ordered_keys) do -- local v = map[k] @@ -56,9 +111,11 @@ local items = { --- made. function TextTransform.popup() state.save_positions() + load_frequency() local filtered = {} - local config = _G.TextTransform.options + print(vim.inspect(_G.TextTransform.config)) + local config = _G.TextTransform.config for _, item in ipairs(items) do if not config.replacers[item.value] or not config.replacers[item.value].enabled then @@ -72,19 +129,13 @@ function TextTransform.popup() prompt_title = "Change Case", finder = finders.new_table({ results = items, - entry_maker = function(entry) - return { - value = entry.value, - display = entry.label, - ordinal = entry.label, - -- ordinal = frequency[entry.value] or 0, - } - end, + entry_maker = entry_maker, }), - sorter = conf.generic_sorter({}), + sorter = frequency_sorter, attach_mappings = function(prompt_bufnr) actions.select_default:replace(function() local selection = action_state.get_selected_entry() + inc_frequency(selection.value) actions.close(prompt_bufnr) vim.schedule(function() replacers.replace_selection(selection.value)