Skip to content

Option for CODEX to ignore certain comments (or code) #21

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
51 changes: 49 additions & 2 deletions python/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,54 @@ def get_first_line_below_cursor_with_text():
return vim_buf[row]
row += 1

# TODO: make adjustable
skip_list = [
"todo",
"fixme",
]

def create_completion(stop=None):
def sanitize_input(input_prompt: str, ignore_skiplist:bool = True) -> str:
"""
Ignores specially marked lines
- the start of a comment containes `skip_list` -> remove the comment
- a comment starting with `codex-ignore` will ignore this and the next line
- `codex-on` and `codex-off` toggle multiple lines
"""
# TODO: support trailing comments (might require syntax parsing)
lines = input_prompt.split('\n')
ignoring = False
ignoring_toggle = False
ignored_lines = []
for i, line in enumerate(lines):
if ignoring:
# WARNING: this prevents this line for being parsable
ignored_lines.append(i)
ignoring = False
continue

tmpline = line.strip()
if not tmpline.startswith('#'):
continue
tmpline = tmpline.removeprefix('#').strip()

if ignoring_toggle:
if tmpline.lower().startswith('codex-off'):
ignoring_toggle = False
elif tmpline.lower().startswith('codex-ignore'):
ignoring = True
elif ignore_skiplist and any(tmpline.lower().startswith(s) for s in skip_list):
# TODO: maybe remove whole comment block?
pass
elif tmpline.lower().startswith('codex-on'):
ignoring_toggle = True
else:
continue

ignored_lines.append(i)
return '\n'.join([lines[i] for i in range(len(lines)) if i not in ignored_lines])


def create_completion(stop=None, ignore_skiplist=False):
try:
from AUTH import ORGANIZATION_ID, SECRET_KEY

Expand All @@ -130,13 +176,14 @@ def create_completion(stop=None):
max_tokens = get_max_tokens()
vim_buf = vim.current.buffer
input_prompt = '\n'.join(vim_buf[:])

row, col = vim.current.window.cursor
input_prompt = '\n'.join(vim_buf[row:])
input_prompt += '\n'.join(vim_buf[:row-1])
input_prompt += '\n' + vim_buf[row-1][:col]
if not stop:
stop = get_first_line_below_cursor_with_text()
input_prompt = sanitize_input(input_prompt, ignore_skiplist=ignore_skiplist)
response = complete_input(input_prompt, stop=stop, max_tokens=max_tokens)
write_response(response, stop=stop)

Expand Down