diff --git a/README.md b/README.md index b4af949..4f4897f 100644 --- a/README.md +++ b/README.md @@ -122,32 +122,33 @@ require("url_open").setup({ }, }, deep_pattern = false, + -- a list of patterns to open url under cursor extra_patterns = { - -- [pattern] = prefix: string only or nil - -- [pattern] = { - -- prefix = "", - -- suffix = "" - -- file_patterns = { "package%.json" }, -- support for only specific file match with pattern - -- excluded_file_patterns = {}, -- exclude file match with pattern - -- extra_condition = function(pattern_found) -- the function will be called when pattern found and return boolean - -- return pattern_found ~= "version" and pattern_found ~= "proxy" - -- end, + -- { + -- pattern = '["]([^%s]*)["]:%s*"[^"]*%d[%d%.]*"', + -- prefix = "https://www.npmjs.com/package/", + -- suffix = "", + -- file_patterns = { "package%.json" }, + -- excluded_file_patterns = nil, + -- extra_condition = function(pattern_found) + -- return not vim.tbl_contains({ "version", "proxy" }, pattern_found) + -- end, -- }, - - -- Ex: ['["]([^%s]*)["]:'] = "https://www.npmjs.com/package/", -- so the url will be https://www.npmjs.com/package/[pattern_found] - -- - -- Ex: ['["]([^%s]*)["]:'] = {prefix = "https://www.npmjs.com/package/", suffix = "/issues"}, - -- so the url will be https://www.npmjs.com/package/[pattern_found]/issues - -- E.g: - -- ['["]([^%s]*)["]:%s*"[^"]*%d[%d%.]*"'] = { - -- prefix = "https://www.npmjs.com/package/", - -- suffix = "", - -- file_patterns = { "package%.json" }, - -- excluded_file_patterns = {}, - -- extra_condition = function() return true end, -- need to return boolean + + -- { + -- pattern = '["]([^%s]*)["]:%s*"[^"]*%d[%d%.]*"', + -- prefix = "https://www.npmjs.com/package/", + -- suffix = "/issues", + -- file_patterns = { "package%.json" }, + -- excluded_file_patterns = nil, + -- extra_condition = function(pattern_found) + -- return not vim.tbl_contains({ "version", "proxy" }, pattern_found) + -- end, -- }, + -- + -- so the url will be https://www.npmjs.com/package/[pattern_found]/issues }, }) ``` diff --git a/docs/index.html b/docs/index.html index 5ad5509..91b5159 100644 --- a/docs/index.html +++ b/docs/index.html @@ -100,7 +100,7 @@

Topics

generated by LDoc 1.5.0 -Last updated 2023-09-23 12:16:00 +Last updated 2023-09-23 15:22:04
diff --git a/docs/modules/url-open.html b/docs/modules/url-open.html index 90c8701..04ce977 100644 --- a/docs/modules/url-open.html +++ b/docs/modules/url-open.html @@ -115,7 +115,7 @@

Usage:

generated by LDoc 1.5.0 -Last updated 2023-09-23 12:16:00 +Last updated 2023-09-23 15:22:04
diff --git a/docs/modules/url-open.modules.autocmd.html b/docs/modules/url-open.modules.autocmd.html index 2cbad8f..b511c4b 100644 --- a/docs/modules/url-open.modules.autocmd.html +++ b/docs/modules/url-open.modules.autocmd.html @@ -117,7 +117,7 @@

Usage:

generated by LDoc 1.5.0 -Last updated 2023-09-23 12:16:00 +Last updated 2023-09-23 15:22:04
diff --git a/docs/modules/url-open.modules.commands.html b/docs/modules/url-open.modules.commands.html index 42cda23..626a904 100644 --- a/docs/modules/url-open.modules.commands.html +++ b/docs/modules/url-open.modules.commands.html @@ -117,7 +117,7 @@

Usage:

generated by LDoc 1.5.0 -Last updated 2023-09-23 12:16:00 +Last updated 2023-09-23 15:22:04
diff --git a/docs/modules/url-open.modules.handlers.html b/docs/modules/url-open.modules.handlers.html index 6bd00ac..3bb37b0 100644 --- a/docs/modules/url-open.modules.handlers.html +++ b/docs/modules/url-open.modules.handlers.html @@ -370,7 +370,7 @@

See also:

generated by LDoc 1.5.0 -Last updated 2023-09-23 12:16:00 +Last updated 2023-09-23 15:22:04
diff --git a/docs/modules/url-open.modules.highlight.html b/docs/modules/url-open.modules.highlight.html index 0e9619f..e9d1ba6 100644 --- a/docs/modules/url-open.modules.highlight.html +++ b/docs/modules/url-open.modules.highlight.html @@ -201,7 +201,7 @@

See also:

generated by LDoc 1.5.0 -Last updated 2023-09-23 12:16:00 +Last updated 2023-09-23 15:22:04
diff --git a/docs/modules/url-open.modules.logger.html b/docs/modules/url-open.modules.logger.html index 94aafe2..18510b9 100644 --- a/docs/modules/url-open.modules.logger.html +++ b/docs/modules/url-open.modules.logger.html @@ -172,7 +172,7 @@

Parameters:

generated by LDoc 1.5.0 -Last updated 2023-09-23 12:16:00 +Last updated 2023-09-23 15:22:04
diff --git a/docs/modules/url-open.modules.options.html b/docs/modules/url-open.modules.options.html index 0653299..ec6e014 100644 --- a/docs/modules/url-open.modules.options.html +++ b/docs/modules/url-open.modules.options.html @@ -192,7 +192,7 @@

Fields:

generated by LDoc 1.5.0 -Last updated 2023-09-23 12:16:00 +Last updated 2023-09-23 15:22:04
diff --git a/docs/modules/url-open.modules.patterns.html b/docs/modules/url-open.modules.patterns.html index 152e7a5..7ef2293 100644 --- a/docs/modules/url-open.modules.patterns.html +++ b/docs/modules/url-open.modules.patterns.html @@ -69,14 +69,14 @@

Tables

- +
PATTERNSDefault Patterns to match urls.Default patterns to match urls

Fields

- +
DEEP_PATTERNDeep Pattern to match urls from text.Deep Pattern to match URLs from text.
@@ -92,99 +92,34 @@

Tables

PATTERNS
- Default Patterns to match urls.

- -

Http(s) URL pattern - Matches URLs starting with "http://" or "https://" - Example: "http://example.com", "https://www.example.com" - Pattern: "(https?://[%w-%.]+%.%w[%w-%.%%%?%.:/+=&%%[%]#<>]*)" - Prefix: "" - Suffix: "" - Filepatterns: All files - Extracondition: None - Excludedfilepatterns: None - Note: This pattern is used to match urls in all files

- - -

Npm Package pattern - Matches npm package names - Example: "react", "react-dom" - Pattern: '"["]:%s"[^"]%d[%d%.]*"' - Prefix: "https://www.npmjs.com/package/" - Suffix: "" - Filepatterns: "package%.json" - Extracondition: patternfound ~= "version" and patternfound ~= "proxy" - Excludedfilepatterns: None - Note: This pattern is used to match npm packages in package.json files

- -

Git Plugin pattern - Matches git plugin names - Example: "airblade/vim-gitgutter", "tpope/vim-fugitive" - Pattern: "\"'[\"']" - Prefix: "https://github.com/" - Suffix: "" - Filepatterns: All files except package.json and package-lock.json - Extracondition: None - Excludedfilepatterns: "package%.json", "package%-lock%.json" - Note: This pattern is used to match git plugins in all files except package.json and package-lock.json

- - -

Brew Formula pattern - Matches brew formula names - Example: "bat", "exa" - Pattern: 'brew "["]' - Prefix: "https://formulae.brew.sh/formula/" - Suffix: "" - Filepatterns: All files - Extracondition: None - Excludedfilepatterns: None - Note: This pattern is used to match brew formulas in all files

- -

Cask Formula pattern - Matches cask formula names - Example: "firefox", "google-chrome" - Pattern: 'cask "["]' - Prefix: "https://formulae.brew.sh/cask/" - Suffix: "" - Filepatterns: All files - Extracondition: None - Excludedfilepatterns: None - Note: This pattern is used to match cask formulas in all files

- -

Cargo Package pattern - Matches cargo package names - Example: "serde", "serdejson" - Pattern: "^%s*([%w]+)%s*=" - Prefix: "https://crates.io/crates/" - Suffix: "" - Filepatterns: "Cargo%.toml" - Extracondition: not vim.tblcontains({ - "name", - "version", - "edition", - "authors", - "description", - "license", - "repository", - "homepage", - "documentation", - "keywords", - }, patternfound) - Excludedfilepatterns: None - Note: This pattern is used to match cargo packages in Cargo.toml files + Default patterns to match urls

Fields:

@@ -202,18 +137,24 @@

Fields

DEEP_PATTERN
- Deep Pattern to match urls from text. This pattern will find urls in the following formats: - http://example.com - https://www.example.com - ftp://ftp.example.com - file:///path/to/file.txt - ssh://user@hostname - git://github.com/user/repo - http://example.com/path?param=value - https://www.example.com/another/path#section - http://example.com:8080 - https://www.example.com:8443 - ftp://ftp.example.com:2121 + +

Deep Pattern to match URLs from text. This pattern will find URLs in various formats. + Supported URL formats:

+ + + @@ -229,7 +170,7 @@

Fields

generated by LDoc 1.5.0 -Last updated 2023-09-23 12:16:00 +Last updated 2023-09-23 15:22:04
diff --git a/docs/topics/README.md.html b/docs/topics/README.md.html index 5cbf3e5..daece0f 100644 --- a/docs/topics/README.md.html +++ b/docs/topics/README.md.html @@ -87,7 +87,8 @@

Features

  • ✈️ Open the GitHub page for the Neovim plugin mentioned under the cursor (e.g. Plug 'nvim-lua/plenary.nvim', "sontungexpt/url-open").
  • 🍨 Easily open the npm package specified in the package.json file. (e.g. "lodash": "^4.17.21",).
  • -
  • Extend support for recognized formats, including brew formulas and casks.
  • +
  • 🍻 Open the Homebrew formula or cask specified in the Brewfile.
  • +
  • 🍕 Open the cargo package specified in the Cargo.toml file.
  • 🚀 Provide an optional deep pattern matching feature, which can be enabled, to accurately identify and handle various URL formats, such as:
  • http://example.com
  • @@ -190,32 +191,33 @@

    Configuration

    }, }, deep_pattern = false, - extra_patterns = { - -- [pattern] = prefix: string only or nil - -- [pattern] = { - -- prefix = "", - -- suffix = "" - -- file_patterns = { "package%.json" }, -- support for only specific file match with pattern - -- excluded_file_patterns = {}, -- exclude file match with pattern - -- extra_condition = function(pattern_found) -- the function will be called when pattern found and return boolean - -- return pattern_found ~= "version" and pattern_found ~= "proxy" - -- end, + -- a list of patterns to open url under cursor + extra_patterns = { + -- { + -- pattern = '["]([^%s]*)["]:%s*"[^"]*%d[%d%.]*"', + -- prefix = "https://www.npmjs.com/package/", + -- suffix = "", + -- file_patterns = { "package%.json" }, + -- excluded_file_patterns = nil, + -- extra_condition = function(pattern_found) + -- return not vim.tbl_contains({ "version", "proxy" }, pattern_found) + -- end, -- }, - - -- Ex: ['["]([^%s]*)["]:'] = "https://www.npmjs.com/package/", -- so the url will be https://www.npmjs.com/package/[pattern_found] - -- - -- Ex: ['["]([^%s]*)["]:'] = {prefix = "https://www.npmjs.com/package/", suffix = "/issues"}, - -- so the url will be https://www.npmjs.com/package/[pattern_found]/issues - -- E.g: - -- ['["]([^%s]*)["]:%s*"[^"]*%d[%d%.]*"'] = { - -- prefix = "https://www.npmjs.com/package/", - -- suffix = "", - -- file_patterns = { "package%.json" }, - -- excluded_file_patterns = {}, - -- extra_condition = function() return true end, -- need to return boolean + + -- { + -- pattern = '["]([^%s]*)["]:%s*"[^"]*%d[%d%.]*"', + -- prefix = "https://www.npmjs.com/package/", + -- suffix = "/issues", + -- file_patterns = { "package%.json" }, + -- excluded_file_patterns = nil, + -- extra_condition = function(pattern_found) + -- return not vim.tbl_contains({ "version", "proxy" }, pattern_found) + -- end, -- }, + -- + -- so the url will be https://www.npmjs.com/package/[pattern_found]/issues }, }) @@ -252,7 +254,7 @@

    License

    generated by LDoc 1.5.0 -Last updated 2023-09-23 12:16:00 +Last updated 2023-09-23 15:22:04
    diff --git a/lua/url-open/modules/handlers.lua b/lua/url-open/modules/handlers.lua index 1557833..c5c3e44 100644 --- a/lua/url-open/modules/handlers.lua +++ b/lua/url-open/modules/handlers.lua @@ -59,26 +59,24 @@ M.find_first_url_matching_patterns = function(text, patterns, start_pos, found_u found_url_smaller_pos = found_url_smaller_pos or #text local start_found, end_found, url_found = nil, nil, nil - for pattern, subs in pairs(patterns) do - subs = subs or { prefix = "" } - if type(subs) == "string" then subs = { prefix = subs } end -- support old version - + for _, cond in ipairs(patterns) do if - not M.check_file_patterns(subs.excluded_file_patterns, true) - and M.check_file_patterns(subs.file_patterns) + not M.check_file_patterns(cond.excluded_file_patterns, true) + and M.check_file_patterns(cond.file_patterns) then - local start_pos_result, end_pos_result, url = text:find(pattern, start_pos) + local start_pos_result, end_pos_result, url = text:find(cond.pattern, start_pos) if url and found_url_smaller_pos > start_pos_result - and M.check_condition_pattern(url, subs.extra_condition) + and M.check_condition_pattern(url, cond.extra_condition) then found_url_smaller_pos = start_pos_result - url_found = (subs.prefix or "") .. url .. (subs.suffix or "") + url_found = (cond.prefix or "") .. url .. (cond.suffix or "") start_found, end_found = start_pos_result, end_pos_result end end end + return start_found, end_found, url_found end diff --git a/lua/url-open/modules/options.lua b/lua/url-open/modules/options.lua index d467ea9..e41b989 100644 --- a/lua/url-open/modules/options.lua +++ b/lua/url-open/modules/options.lua @@ -69,10 +69,9 @@ M.validate_opts = function(opts) end if opts.extra_patterns then - for pattern, sub in pairs(opts.extra_patterns) do + for _, cond in ipairs(opts.extra_patterns) do validate { - pattern = { pattern, "string" }, - sub = { sub, { "table", "string" } }, + pattern = { cond.pattern, "string" }, } end end diff --git a/lua/url-open/modules/patterns.lua b/lua/url-open/modules/patterns.lua index cbe5ff9..eea7bc9 100644 --- a/lua/url-open/modules/patterns.lua +++ b/lua/url-open/modules/patterns.lua @@ -2,129 +2,81 @@ -- local M = {} ---- Deep Pattern to match urls from text. This pattern will find urls in the following formats: --- http://example.com --- https://www.example.com --- ftp://ftp.example.com --- file:///path/to/file.txt --- ssh://user@hostname --- git://github.com/user/repo --- http://example.com/path?param=value --- https://www.example.com/another/path#section --- http://example.com:8080 --- https://www.example.com:8443 --- ftp://ftp.example.com:2121 +--- Deep Pattern to match URLs from text. This pattern will find URLs in various formats. +--- Supported URL formats: +-- +-- - http://example.com +-- - https://www.example.com +-- - ftp://ftp.example.com +-- - file:///path/to/file.txt +-- - ssh://user@hostname +-- - git://github.com/user/repo +-- - http://example.com/path?param=value +-- - https://www.example.com/another/path#section +-- - http://example.com:8080 +-- - https://www.example.com:8443 +-- - ftp://ftp.example.com:2121 M.DEEP_PATTERN = "\\v\\c%(%(h?ttps?|ftp|file|ssh|git)://|[a-z]+[@][a-z]+[.][a-z]+:)%([&:#*@~%<>_\\-=?!+;/0-9a-z]+%(%([.;/?]|[.][.]+)[&:#*@~%<>_\\-=?!+/0-9a-z]+|:\\d+|,%(%(%(h?ttps?|ftp|file|ssh|git)://|[a-z]+[@][a-z]+[.][a-z]+:)@![0-9a-z]+))*|\\([&:#*@~%_\\-=?!+;/.0-9a-z]*\\)|\\[[&:#*@~%_\\-=?!+;/.0-9a-z]*\\]|\\{%([&:#*@~%_\\-=?!+;/.0-9a-z]*|\\{[&:#*@~%_\\-=?!+;/.0-9a-z]*\\})\\})+" --- --- Default Patterns to match urls. --- --- Http(s) URL pattern --- Matches URLs starting with "http://" or "https://" --- Example: "http://example.com", "https://www.example.com" --- Pattern: "(https?://[%w-_%.]+%.%w[%w-_%.%%%?%.:/+=&%%[%]#<>]*)" --- Prefix: "" --- Suffix: "" --- File_patterns: All files --- Extra_condition: None --- Excluded_file_patterns: None --- Note: This pattern is used to match urls in all files --- --- --- Npm Package pattern --- Matches npm package names --- Example: "react", "react-dom" --- Pattern: '["]([^%s]*)["]:%s*"[^"]*%d[%d%.]*"' --- Prefix: "https://www.npmjs.com/package/" --- Suffix: "" --- File_patterns: "package%.json" --- Extra_condition: pattern_found ~= "version" and pattern_found ~= "proxy" --- Excluded_file_patterns: None --- Note: This pattern is used to match npm packages in package.json files --- --- Git Plugin pattern --- Matches git plugin names --- Example: "airblade/vim-gitgutter", "tpope/vim-fugitive" --- Pattern: "[\"']([^%s~/]*/[^%s~/]*)[\"']" --- Prefix: "https://github.com/" --- Suffix: "" --- File_patterns: All files except package.json and package-lock.json --- Extra_condition: None --- Excluded_file_patterns: "package%.json", "package%-lock%.json" --- Note: This pattern is used to match git plugins in all files except package.json and package-lock.json --- --- --- Brew Formula pattern --- Matches brew formula names --- Example: "bat", "exa" --- Pattern: 'brew ["]([^%s]*)["]' --- Prefix: "https://formulae.brew.sh/formula/" --- Suffix: "" --- File_patterns: All files --- Extra_condition: None --- Excluded_file_patterns: None --- Note: This pattern is used to match brew formulas in all files --- --- Cask Formula pattern --- Matches cask formula names --- Example: "firefox", "google-chrome" --- Pattern: 'cask ["]([^%s]*)["]' --- Prefix: "https://formulae.brew.sh/cask/" --- Suffix: "" --- File_patterns: All files --- Extra_condition: None --- Excluded_file_patterns: None --- Note: This pattern is used to match cask formulas in all files --- --- Cargo Package pattern --- Matches cargo package names --- Example: "serde", "serde_json" --- Pattern: "^%s*([%w_]+)%s*=" --- Prefix: "https://crates.io/crates/" --- Suffix: "" --- File_patterns: "Cargo%.toml" --- Extra_condition: not vim.tbl_contains({ --- "name", --- "version", --- "edition", --- "authors", --- "description", --- "license", --- "repository", --- "homepage", --- "documentation", --- "keywords", --- }, pattern_found) --- Excluded_file_patterns: None --- Note: This pattern is used to match cargo packages in Cargo.toml files +--- Default patterns to match urls +--- @table PATTERNS +--- @tfield string pattern : Pattern to match urls (required) +--- @tfield string|nil prefix : Prefix to add to the url +--- @tfield string|nil suffix : Suffix to add to the url +--- @tfield table|string|nil file_patterns : File patterns to match against +--- @tfield table|string|nil excluded_file_patterns : File patterns to exclude +--- @tfield function(pattern_found)|boolean|nil extra_condition : A callback function will be called with the pattern found as argument. If the function returns false, the pattern will be ignored. If the function returns true, the pattern will be used. M.PATTERNS = { - ["(https?://[%w-_%.]+%.%w[%w-_%.%%%?%.:/+=&%%[%]#<>]*)"] = "", --- url http(s) - ['["]([^%s]*)["]:%s*"[^"]*%d[%d%.]*"'] = { + { + pattern = "(https?://[%w-_%.]+%.%w[%w-_%.%%%?%.:/+=&%%[%]#<>]*)", + prefix = "", + suffix = "", + file_patterns = nil, + excluded_file_patterns = nil, + extra_condition = nil, + }, + { + pattern = '["]([^%s]*)["]:%s*"[^"]*%d[%d%.]*"', prefix = "https://www.npmjs.com/package/", suffix = "", file_patterns = { "package%.json" }, + excluded_file_patterns = nil, extra_condition = function(pattern_found) - return pattern_found ~= "version" and pattern_found ~= "proxy" + return not vim.tbl_contains({ "version", "proxy" }, pattern_found) end, - }, -- npm package - ["[\"']([^%s~/]*/[^%s~/]*)[\"']"] = { + }, + { + pattern = "[\"']([^%s~/]*/[^%s~/]*)[\"']", prefix = "https://github.com/", suffix = "", + file_patterns = nil, excluded_file_patterns = { "package%.json", "package%-lock%.json" }, - }, -- plugin name git - ['brew ["]([^%s]*)["]'] = { + extra_condition = nil, + }, + { + pattern = 'brew ["]([^%s]*)["]', prefix = "https://formulae.brew.sh/formula/", suffix = "", - }, -- brew formula - ['cask ["]([^%s]*)["]'] = { + file_patterns = nil, + excluded_file_patterns = nil, + extra_condition = nil, + }, + { + pattern = 'cask ["]([^%s]*)["]', prefix = "https://formulae.brew.sh/cask/", suffix = "", - }, -- cask formula - ["^%s*([%w_]+)%s*="] = { + file_patterns = nil, + excluded_file_patterns = nil, + extra_condition = nil, + }, + { + pattern = "^%s*([%w_]+)%s*=", prefix = "https://crates.io/crates/", suffix = "", file_patterns = { "Cargo%.toml" }, + excluded_file_patterns = nil, extra_condition = function(pattern_found) return not vim.tbl_contains({ "name", @@ -139,7 +91,7 @@ M.PATTERNS = { "keywords", }, pattern_found) end, - }, -- cargo package + }, } return M