Skip to content

Commit

Permalink
feat: remove coredor.nvim dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyasYOY committed Dec 14, 2024
1 parent 1bd915f commit 08ac76f
Show file tree
Hide file tree
Showing 18 changed files with 918 additions and 240 deletions.
1 change: 0 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ jobs:
}
mkdir -p ~/.local/share/nvim/site/pack/vendor/start
git clone --depth 1 https://github.com/nvim-lua/plenary.nvim ~/.local/share/nvim/site/pack/vendor/start/plenary.nvim
git clone --depth 1 https://github.com/IlyasYOY/coredor.nvim ~/.local/share/nvim/site/pack/vendor/start/coredor.nvim
git clone --depth 1 https://github.com/nvim-telescope/telescope.nvim ~/.local/share/nvim/site/pack/vendor/start/telescope.nvim
git clone --depth 1 https://github.com/kyazdani42/nvim-web-devicons ~/.local/share/nvim/site/pack/vendor/start/nvim-web-devicons
ln -s $(pwd) ~/.local/share/nvim/site/pack/vendor/start
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Project is currently in WIP status.

This project requires:

- [IlyasYOY/coredor.nvim: Core utils for nvim](https://github.com/IlyasYOY/coredor.nvim). Utility library I use for my plugins.
- [nvim-lua/plenary.nvim: plenary: full; complete; entire; absolute; unqualified. All the lua functions I don't want to write twice.](https://github.com/nvim-lua/plenary.nvim). Collection of useful utilities: testing, IO, etc.
- [nvim-telescope/telescope.nvim: Find, Filter, Preview, Pick. All lua, all the time.](https://github.com/nvim-telescope/telescope.nvim). Fuzzy-searching.

Expand All @@ -29,7 +28,6 @@ return {
{
"IlyasYOY/obs.nvim",
dependencies = {
"IlyasYOY/coredor.nvim",
"nvim-lua/plenary.nvim",
"nvim-telescope/telescope.nvim",
},
Expand Down
2 changes: 1 addition & 1 deletion lua/obs/cmp-source.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local core = require "coredor"
local core = require "obs.utils"

local source = {}

Expand Down
10 changes: 5 additions & 5 deletions lua/obs/journal.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local Path = require "plenary.path"
local File = require "coredor.file"
local File = require "obs.utils.file"
local telescope = require "obs.telescope"

---journal opts
Expand Down Expand Up @@ -71,15 +71,15 @@ function Journal:find_journal()
end

-- lists journal daily entries
---@return Array<coredor.File>
---@return Array<obs.utils.File>
function Journal:list_dailies()
local path = self._home_path:expand()
local files = File.list(path, self._date_glob .. ".md")
return files
end

-- lists journal weekly entries
---@return Array<coredor.File>
---@return Array<obs.utils.File>
function Journal:list_weeklies()
local path = self._home_path:expand()
local files = File.list(path, self._week_glob .. ".md")
Expand All @@ -88,7 +88,7 @@ end

-- get today note file
---@param create_if_missing boolean?
---@return coredor.File
---@return obs.utils.File
function Journal:today(create_if_missing)
local filename = self._date_provider()
---@type Path
Expand All @@ -110,7 +110,7 @@ end

-- get this week note file
---@param create_if_missing boolean?
---@return coredor.File
---@return obs.utils.File
function Journal:this_week(create_if_missing)
local filename = self._week_provider()
---@type Path
Expand Down
4 changes: 2 additions & 2 deletions lua/obs/journal_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ local Path = require "plenary.path"
local Journal = require "obs.journal"
local Templater = require "obs.templater"

local spec_utils = require "coredor.spec"
local core = require "coredor"
local spec_utils = require "obs.utils.spec"
local core = require "obs.utils"

local function journal_fixture()
local result = {}
Expand Down
2 changes: 1 addition & 1 deletion lua/obs/link_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local Link = require "obs.link"
require "coredor.spec"
require "obs.utils.spec"

---@param link obs.Link
---@param header string?
Expand Down
6 changes: 3 additions & 3 deletions lua/obs/templater.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local Path = require "plenary.path"
local core = require "coredor"
local File = require "coredor.file"
local core = require "obs.utils"
local File = require "obs.utils.file"
local telescope = require "obs.telescope"

-- options for VarProvider
Expand Down Expand Up @@ -97,7 +97,7 @@ function Templater:search_and_insert_template()
end

-- lists templates
---@return coredor.File[]
---@return obs.utils.File[]
function Templater:list_templates()
local home_path_string = self._home_path:expand()
return File.list(home_path_string, "*.md")
Expand Down
2 changes: 1 addition & 1 deletion lua/obs/templater_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local Templater = require "obs.templater"
local spec_utils = require "coredor.spec"
local spec_utils = require "obs.utils.spec"

describe("proccess files", function()
local templater
Expand Down
78 changes: 0 additions & 78 deletions lua/obs/utils.lua

This file was deleted.

105 changes: 105 additions & 0 deletions lua/obs/utils/file.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
local utils = require "obs.utils"
local Path = require "plenary.path"

---Simple file wrapper
---TODO: Tests.
--
---@class obs.utils.File
---@field private _plenary_path Path
local File = {}
File.__index = File

---gets file name with extension
---@package
---@return string?
function File:get_name_with_extension()
local path_split = utils.string_split(self:path(), "/")
local name_with_extension = path_split[#path_split]
return name_with_extension
end

---gets file name with out extension
---@package
---@return string?
function File:get_name_with_out_extension()
local name_with_extension = self:get_name_with_extension()
if name_with_extension == nil then
return nil
end

local name_with_extension_split =
utils.string_split(name_with_extension, ".")
name_with_extension_split[#name_with_extension_split] = nil
local name_with_out_extension =
utils.string_merge(name_with_extension_split, ".")

if name_with_out_extension == "" then
return name_with_extension
end
return name_with_out_extension
end

---name of the file with out extension
---@return string?
function File:name()
return self:get_name_with_out_extension()
end

---returns path to a file
---@return string?
function File:path()
return self._plenary_path:expand()
end

--- lists files from path matching glob pattern
---@param path string
---@param glob string
---@return obs.utils.File[]
function File.list(path, glob)
-- NOTE: Speed this up a bit. Maybe I should use `plenary.scandir`.
local files_as_text = vim.fn.globpath(path, glob)
local files_pathes = utils.string_split(files_as_text, "\n")
local results = utils.array_map(files_pathes, function(file_path)
return File:new(file_path)
end)
return results
end

--- creates file wrapper
---@param path string
---@return obs.utils.File
function File:new(path)
return setmetatable({
_plenary_path = Path:new(path),
}, self)
end

---return plenary object
---@return Path
function File:as_plenary()
return self._plenary_path
end

---Reads file content as string
---@return string?
function File:read()
return self._plenary_path:read()
end

---Opens file in buffer for editing
function File:edit()
vim.fn.execute("edit " .. self:path())
end

---renames current file. Different from mv, it does the renaming only of the file name.
--- /path/test -> to "new" -> /path/new.
--- check tests for details.
---@param new_name string name of the file
---@return boolean
function File:change_name(new_name)
local parent = self._plenary_path:parent()
local new_file_path = parent / new_name
self._plenary_path:rename { new_name = new_file_path:expand() }
end

return File
69 changes: 69 additions & 0 deletions lua/obs/utils/file_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---@module "luassert"
---@module "plenary.busted"

local spec = require "obs.utils.spec"
local File = require "obs.utils.file"

describe("change file name", function()
local dir_fixture = spec.temp_dir_fixture()

---resolves file in test dit with specified name
---@param name string
---@return obs.utils.File
local function resolve_file_with_name(name)
---@type Path
local file_path = dir_fixture.path / name
return File:new(file_path:expand())
end

---creates fime in test dir with specified name
---@param name string
---@return obs.utils.File
local function create_file_with_name(name)
local file = resolve_file_with_name(name)
file:as_plenary():touch()
return file
end

it("should rename", function()
local file = create_file_with_name "test"

local expected_name = "new name"

file:change_name(expected_name)

assert.file(
file,
expected_name,
resolve_file_with_name(expected_name):path()
)
end)

it("should rename nested directory", function()
local file = create_file_with_name "test.txt"

local expected_name = "cool/new name"

file:change_name(expected_name)

assert.file(
file,
"new name",
resolve_file_with_name(expected_name):path()
)
end)

it("should rename with extension", function()
local file = create_file_with_name "test.txt"

local expected_name = "new name.txt"

file:change_name(expected_name)

assert.file(
file,
"new name",
resolve_file_with_name(expected_name):path()
)
end)
end)
Loading

0 comments on commit 08ac76f

Please sign in to comment.