Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

**Title**: Implement Functions to Extract and Handle Code from Markdown in Vim #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions plugin/chatvim.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ class GPTPlugin:
def __init__(self, nvim):
self.nvim = nvim

@pynvim.function("ExtractCodeFromMarkdown", sync=True)
Copy link
Owner

@jakethekoenig jakethekoenig Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Can you reply with a joke?

Copy link
Owner

@jakethekoenig jakethekoenig Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Why do programmers prefer dark mode? Because light attracts bugs! 😄

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, can you reply with another! @mentat-butler

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Why do Java developers wear glasses? Because they don't see sharp! 😄

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell me another @MentatBot?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly! Here's another programming joke for you:

Why do programmers always mix up Halloween and Christmas?
Because Oct 31 == Dec 25! 😄

This joke plays on the fact that in octal (base 8) notation, 31 is equal to 25 in decimal (base 10). It's a bit of a nerdy one, but I hope you find it amusing!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm glad you enjoyed the jokes! Here's one more for you:

Why do programmers prefer iOS development? Because it's a Swift process! 😄

This joke plays on the double meaning of 'Swift' - both as a programming language used for iOS development and as an adjective meaning 'quick' or 'fast'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm glad you enjoyed the jokes! Here's one more for you:

Why do programmers prefer iOS development? Because it's a Swift process! 😄

This joke plays on the double meaning of 'Swift' - both as a programming language used for iOS development and as an adjective meaning 'quick' or 'fast'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding error handling for invalid register names and a docstring explaining the function's purpose and parameters. For example:

@pynvim.function("ExtractCodeFromMarkdown", sync=True)
def extract_code_from_markdown(self, args):
    """Extract code blocks from markdown and store in a vim register.
    
    Args:
        args: List containing optional register name. Defaults to '"'.
    """
    if not args:
        register = '"'
    elif not isinstance(args[0], str) or len(args[0]) != 1:
        self.nvim.command('echoerr "Register must be a single character"')
        return
    else:
        register = args[0]

def extract_code_from_markdown(self, args):
register = args[0] if args else '"'
lines = self.nvim.current.buffer[:]
code_block = []
in_code_block = False
for line in lines:
if line.strip() == '```':
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation doesn't handle language specifiers in code blocks. Consider updating this to handle cases like ```python:

if line.strip().startswith('```'):
    in_code_block = not in_code_block
    continue

in_code_block = not in_code_block
continue
if in_code_block:
code_block.append(line)
if code_block:
self.nvim.command(f'let @{register} = "{chr(10).join(code_block)}"')
else:
self.nvim.command('echo "No markdown code block found"')

@pynvim.function("PasteCodeToNewFile", sync=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded filename 'llm_code.py' assumes Python code. Consider:

  1. Adding a parameter for the filename
  2. Making the file extension match the language specified in the code block (if implemented)
  3. Adding a docstring

For example:

@pynvim.function("PasteCodeToNewFile", sync=True)
def paste_code_to_new_file(self, args):
    """Paste code from a register into a new file.
    
    Args:
        args: List containing [register, filename]. Both optional.
              register defaults to '"', filename defaults to 'llm_code.py'
    """
    register = args[0] if len(args) > 0 else '"'
    filename = args[1] if len(args) > 1 else 'llm_code.py'
    
    self.nvim.command('enew')  # Open a new buffer
    self.nvim.command(f'put @{register}')  # Paste contents of the register
    self.nvim.command(f'write {filename}')  # Save the file
    self.nvim.command(f'echo "Code block saved to {filename}"')

def paste_code_to_new_file(self, args):
register = args[0] if args else '"'
self.nvim.command('enew') # Open a new buffer
self.nvim.command(f'put @{register}') # Paste contents of the register
self.nvim.command('write llm_code.py') # Save the file
self.nvim.command('echo "Code block saved to llm_code.py"')


@pynvim.function("GPTResponse")
def gpt_response(self, args):
Expand Down