local preparetext = function(inputtext) local formatted = inputtext formatted =…
- User
local prepare_text = function(input_text) local formatted = input_text formatted = formatted:gsub(" ", "") formatted = formatted:gsub("Ü", "Ue") formatted = formatted:gsub("Ä", "Ae") formatted = formatted:gsub("Ö", "Oe") formatted = formatted:gsub("ü", "ue") formatted = formatted:gsub("ä", "ae") formatted = formatted:gsub("ö", "oe") formatted = formatted:gsub("ß", "ss") formatted = formatted:gsub("'", "") formatted = formatted:gsub(",", "") formatted = formatted:gsub("-", "") formatted = formatted:gsub(";", "") formatted = formatted:gsub("&", "") formatted = formatted:lower() return formatted end
M.create_markdown_link = function(bool) local cursor_col = vim.fn.col(".") - 1 local current_line = vim.fn.getline(".") local text_before_cursor = current_line:sub(1, cursor_col) local text_after_cursor = current_line:sub(cursor_col + 1) local reversed_text = text_before_cursor:reverse()
if reversed_text:find("^%s") then return end
if reversed_text:find("^[^%)]") then local word_to_link = reversed_text:match("[%wäÄüÜöÖß'&%-%.]*") local formatted_link = word_to_link:reverse() formatted_link = prepare_text(formatted_link) formatted_link = formatted_link:reverse() local markdown_link = ")dm." .. formatted_link .. "(]" .. word_to_link .. "[" word_to_link = word_to_link:gsub("%-", "%%-") reversed_text = reversed_text:gsub(word_to_link, markdown_link) local new_line = reversed_text:reverse() vim.fn.setline(".", new_line .. text_after_cursor) vim.cmd("norm f)") vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, true, true), "i", true) return end
local link_pattern = "%)dm%.([^%(]-)%(%]([^%[]-)%[" local word_pattern = " (%S*)" local existing_link, link_text, new_word = reversed_text:match(link_pattern .. word_pattern)
if new_word == nil then return end if new_word:match("%)[^%(]-%(%].-") then return end
if new_word then local formatted_word = new_word:reverse() formatted_word = prepare_text(formatted_word) formatted_word = formatted_word:reverse() local old_pattern = "%)dm%." .. existing_link .. "%(%]" .. link_text .. "%[ " .. new_word local new_link = "" if bool then new_link = ")dm." .. existing_link .. "_" .. formatted_word .. "(]" .. link_text .. " " .. new_word .. "[" else new_link = ")dm." .. existing_link .. "(]" .. link_text .. " " .. new_word .. "[" end reversed_text = reversed_text:gsub(old_pattern, new_link) end local final_line = reversed_text:reverse() vim.fn.setline(".", final_line .. text_after_cursor) vim.fn.cursor(0, #vim.fn.getline(".") - #text_after_cursor + 1) end
Assistant
This Lua code defines a function
M.create_markdown_linkthat assists in creating and modifying Markdown links within a Neovim editor. Let's break down its functionality step by step:1.
prepare_textFunction:- Purpose: This helper function takes a string as input and applies a series of substitutions to format it for use in a Markdown link.
- Substitutions:
- Replaces spaces with underscores.
- Replaces German umlauts (Ü, Ä, Ö, ü, ä, ö) with their transliterated equivalents (Ue, Ae, Oe, ue, ae, oe).
- Replaces 'ß' with 'ss'.
- Removes apostrophes, commas, hyphens, semicolons, and ampersands.
- Converts the entire string to lowercase.
- Return: The formatted string.
2.
M.create_markdown_linkFunction:- Purpose: This function is designed to be used within Neovim to create or modify Markdown links based on the text before the cursor.
- Steps:
- Cursor Position and Text Extraction: It gets the current cursor column and the text before and after the cursor on the current line.
- Reversed Text Analysis: It reverses the text before the cursor for easier pattern matching from the end.
- New Link Creation:
- Check for Leading Space: If the reversed text starts with a space, the function returns without doing anything.
- Find Word to Link: It looks for a word (including German characters and some punctuation) before the cursor.
- Format and Create Link: It reverses the found word, formats it using
prepare_text, reverses it back, and constructs a Markdown link in the format:)dm.<formatted_word>(]<word>[. - Replace and Insert: It replaces the original word with the new Markdown link in the reversed text, reverses the text back, and updates the current line.
- Cursor Placement: It moves the cursor to the closing parenthesis of the new link and simulates a right arrow key press to position the cursor after the link.
- Existing Link Modification:
- Pattern Matching: It looks for an existing Markdown link pattern followed by a word.
- Conditional Formatting: If a new word is found and it doesn't contain a closing parenthesis followed by non-parenthesis characters, it proceeds.
- Format New Word: It reverses the new word, formats it, and reverses it back.
- Update Link: If
boolis true, it appends the formatted new word to the existing link's URL, separated by an underscore. Otherwise, it keeps the original link URL. - Replace and Update: It replaces the old link pattern with the new link in the reversed text, reverses the text back, and updates the current line.
- Cursor Placement: It moves the cursor to the end of the modified line.
Key Points:
-
Neovim Specific: This code relies on Neovim's API (
vim.fn,vim.cmd,vim.api.nvim_feedkeys) and is designed to work within the Neovim editor. -
Markdown Link Format: The code uses a specific Markdown link format:
)dm.<url>(]<text>[. This might be a custom format or a variation of standard Markdown links. -
German Character Support: The
prepare_textfunction specifically handles German umlauts and 'ß', making it suitable for texts containing these characters. -
Cursor Manipulation: The function carefully manipulates the cursor position to ensure the user is left in a convenient position after link creation or modification.
Potential Improvements:
- Error Handling: Add error handling for cases where pattern matching fails or unexpected input is encountered.
- Configuration Options: Allow users to customize the Markdown link format and the character substitutions in
prepare_text. - Documentation: Provide clear documentation explaining the function's purpose, usage, and the expected Markdown link format.
- User
can you rewrite this to work without reversing the text by finding the positions of spaces and use this to create or append markdown links. That should be siimpler, at least in my imagination.
Assistant
Here’s a rewritten version of the code that works without reversing the text. Instead, it uses string manipulation and position tracking to create or append Markdown links. This approach is more straightforward and avoids the complexity of reversing text.
local prepare_text = function(input_text) local formatted = input_text formatted = formatted:gsub(" ", "_") formatted = formatted:gsub("Ü", "Ue") formatted = formatted:gsub("Ä", "Ae") formatted = formatted:gsub("Ö", "Oe") formatted = formatted:gsub("ü", "ue") formatted = formatted:gsub("ä", "ae") formatted = formatted:gsub("ö", "oe") formatted = formatted:gsub("ß", "ss") formatted = formatted:gsub("'", "") formatted = formatted:gsub(",", "") formatted = formatted:gsub("-", "") formatted = formatted:gsub(";", "") formatted = formatted:gsub("&", "_") formatted = formatted:lower() return formatted end M.create_markdown_link = function(bool) local cursor_col = vim.fn.col(".") local current_line = vim.fn.getline(".") local text_before_cursor = current_line:sub(1, cursor_col - 1) local text_after_cursor = current_line:sub(cursor_col) -- Find the word before the cursor local word_start, word_end = nil, nil for i = cursor_col - 1, 1, -1 do local char = current_line:sub(i, i) if char:match("%s") then word_end = i + 1 break end end if not word_end then word_end = 1 end word_start = word_end for i = word_end - 1, 1, -1 do local char = current_line:sub(i, i) if char:match("%s") then word_start = i + 1 break end end if word_start > word_end then return end local word_to_link = current_line:sub(word_start, word_end - 1) if not word_to_link or #word_to_link == 0 then return end -- Create or append Markdown link local formatted_link = prepare_text(word_to_link) local markdown_link = string.format(")[%s](dm.%s", word_to_link, formatted_link) -- Check if the word is already part of a link local link_pattern = string.format("%f[%w]-%[.-%](dm%.%-?%w[_%w]*%)", word_to_link:gsub("([%-%]])", "%%%1")) local existing_link = text_before_cursor:match(link_pattern .. "$") if existing_link then -- Append to existing link if bool is true if bool then local existing_url = existing_link:match("%((dm%.-)%)") local new_url = existing_url:gsub("dm%.", "dm." .. existing_url:match("dm%.(.+)") .. "_" .. formatted_link) markdown_link = existing_link:gsub("%(dm%.%-?%w[_%w]*%)", "(" .. new_url .. ")") else -- Do nothing if bool is false and link already exists return end end -- Insert the Markdown link local new_line = text_before_cursor:sub(1, word_start - 2) .. markdown_link .. text_before_cursor:sub(word_end) .. text_after_cursor vim.fn.setline(".", new_line) vim.fn.cursor(0, word_start + #markdown_link - 1) endKey Changes and Improvements:
-
Position Tracking:
- Instead of reversing the text, the code now tracks the start and end positions of the word before the cursor by iterating backward from the cursor position.
-
Link Creation:
- The Markdown link is created directly using the identified word and formatted using
prepare_text.
- The Markdown link is created directly using the identified word and formatted using
-
Existing Link Handling:
- The code checks if the word is already part of a Markdown link using a pattern match. If
boolistrue, it appends the new word to the existing link's URL.
- The code checks if the word is already part of a Markdown link using a pattern match. If
-
Simpler Logic:
- The logic is more linear and avoids the complexity of reversing and re-reversing text, making it easier to understand and maintain.
Usage:
- Call
M.create_markdown_link(true)to append to existing links or create new ones. - Call
M.create_markdown_link(false)to create new links without modifying existing ones.
This version should be more intuitive and efficient for handling Markdown links in Neovim.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.