From 3f74bb74f6859e000a19c37e0225f3cae51a3804 Mon Sep 17 00:00:00 2001 From: gptlang <121417512+gptlang@users.noreply.github.com> Date: Fri, 2 Feb 2024 18:16:39 +0000 Subject: [PATCH] Add pre-commit for black, flake8, and prettier --- .all-contributorsrc | 26 ++++----------- .flake8 | 3 ++ .luarc.json | 7 ++-- .pre-commit-config.yaml | 17 ++++++++++ README.md | 1 + renovate.json | 4 +-- rplugin/python3/copilot-agent.py | 4 +-- rplugin/python3/copilot-plugin.py | 4 ++- rplugin/python3/copilot.py | 32 ++++++++++++------- .../python3/handlers/vsplit_chat_handler.py | 6 ++-- .../python3/mypynvim/core/autocmdmapper.py | 1 + rplugin/python3/mypynvim/core/keymapper.py | 3 +- rplugin/python3/mypynvim/core/nvim.py | 4 +-- rplugin/python3/mypynvim/core/window.py | 2 ++ .../mypynvim/ui_components/calculator.py | 3 +- .../python3/mypynvim/ui_components/layout.py | 7 ++-- .../python3/mypynvim/ui_components/popup.py | 11 ++++--- .../python3/mypynvim/ui_components/types.py | 2 +- rplugin/python3/utilities.py | 7 ++-- 19 files changed, 82 insertions(+), 62 deletions(-) create mode 100644 .flake8 create mode 100644 .pre-commit-config.yaml diff --git a/.all-contributorsrc b/.all-contributorsrc index 8fd9397a..4f26752b 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -1,7 +1,5 @@ { - "files": [ - "README.md" - ], + "files": ["README.md"], "imageSize": 100, "commit": false, "commitType": "docs", @@ -12,47 +10,35 @@ "name": "gptlang", "avatar_url": "https://avatars.githubusercontent.com/u/121417512?v=4", "profile": "https://github.com/gptlang", - "contributions": [ - "code", - "doc" - ] + "contributions": ["code", "doc"] }, { "login": "jellydn", "name": "Dung Duc Huynh (Kaka)", "avatar_url": "https://avatars.githubusercontent.com/u/870029?v=4", "profile": "https://productsway.com/", - "contributions": [ - "code", - "doc" - ] + "contributions": ["code", "doc"] }, { "login": "qoobes", "name": "Ahmed Haracic", "avatar_url": "https://avatars.githubusercontent.com/u/58834655?v=4", "profile": "https://qoobes.dev", - "contributions": [ - "code" - ] + "contributions": ["code"] }, { "login": "ziontee113", "name": "Trí Thiện Nguyễn", "avatar_url": "https://avatars.githubusercontent.com/u/102876811?v=4", "profile": "https://youtube.com/@ziontee113", - "contributions": [ - "code" - ] + "contributions": ["code"] }, { "login": "Cassius0924", "name": "He Zhizhou", "avatar_url": "https://avatars.githubusercontent.com/u/62874592?v=4", "profile": "https://github.com/Cassius0924", - "contributions": [ - "code" - ] + "contributions": ["code"] } ], "contributorsPerLine": 7, diff --git a/.flake8 b/.flake8 new file mode 100644 index 00000000..cadcae03 --- /dev/null +++ b/.flake8 @@ -0,0 +1,3 @@ +[flake8] +max-line-length = 88 +ignore = E203, E501, W503 diff --git a/.luarc.json b/.luarc.json index d213f255..04ea467f 100644 --- a/.luarc.json +++ b/.luarc.json @@ -1,6 +1,3 @@ { - "diagnostics.globals": [ - "describe", - "it" - ] -} \ No newline at end of file + "diagnostics.globals": ["describe", "it"] +} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..93e78079 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +repos: + - repo: https://github.com/psf/black + rev: "23.10.0" + hooks: + - id: black + - repo: https://github.com/PyCQA/isort + rev: "5.12.0" + hooks: + - id: isort + - repo: https://github.com/PyCQA/flake8 + rev: "6.1.0" + hooks: + - id: flake8 + - repo: https://github.com/pre-commit/mirrors-prettier + rev: "fc260393cc4ec09f8fc0a5ba4437f481c8b55dc1" + hooks: + - id: prettier diff --git a/README.md b/README.md index ca392b1b..c27ea5cc 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ It will prompt you with instructions on your first start. If you already have `C 1. `pip install python-dotenv requests pynvim==0.5.0 prompt-toolkit` 2. `pip install tiktoken` (optional for displaying prompt token counts) 3. Put it in your lazy setup + ```lua return { { diff --git a/renovate.json b/renovate.json index 5db72dd6..22a99432 100644 --- a/renovate.json +++ b/renovate.json @@ -1,6 +1,4 @@ { "$schema": "https://docs.renovatebot.com/renovate-schema.json", - "extends": [ - "config:recommended" - ] + "extends": ["config:recommended"] } diff --git a/rplugin/python3/copilot-agent.py b/rplugin/python3/copilot-agent.py index b8480870..3ac008df 100644 --- a/rplugin/python3/copilot-agent.py +++ b/rplugin/python3/copilot-agent.py @@ -33,7 +33,7 @@ def copilot_agent_visual_cmd(self, args: list[str], range: list[int]): self.init_vsplit_chat_handler() if self.vsplit_chat_handler: file_type = self.nvim.current.buffer.options["filetype"] - code_lines = self.nvim.current.buffer[range[0] - 1: range[1]] + code_lines = self.nvim.current.buffer[range[0] - 1 : range[1]] code = "\n".join(code_lines) self.vsplit_chat_handler.vsplit() self.vsplit_chat_handler.chat(args[0], file_type, code) @@ -58,7 +58,7 @@ def inplace_cmd(self, args: list[str], range: list[int]): self.init_inplace_chat_handler() if self.inplace_chat_handler: file_type = self.nvim.current.buffer.options["filetype"] - code_lines = self.nvim.current.buffer[range[0] - 1: range[1]] + code_lines = self.nvim.current.buffer[range[0] - 1 : range[1]] code = "\n".join(code_lines) user_buffer = self.nvim.current.buffer self.inplace_chat_handler.mount(code, file_type, range, user_buffer) diff --git a/rplugin/python3/copilot-plugin.py b/rplugin/python3/copilot-plugin.py index 8797096f..a0281c29 100644 --- a/rplugin/python3/copilot-plugin.py +++ b/rplugin/python3/copilot-plugin.py @@ -58,7 +58,9 @@ def copilotChat(self, args: List[str]): buffers = self.nvim.buffers existing_buffer = next( - (buf for buf in buffers if os.path.basename(buf.name) == "CopilotChat"), None) + (buf for buf in buffers if os.path.basename(buf.name) == "CopilotChat"), + None, + ) # Check if we're already in a chat buffer if existing_buffer is None: diff --git a/rplugin/python3/copilot.py b/rplugin/python3/copilot.py index 39504bbc..ee471b64 100644 --- a/rplugin/python3/copilot.py +++ b/rplugin/python3/copilot.py @@ -1,16 +1,16 @@ -import requests -import dotenv +import json import os -import uuid import time -import json +import uuid +from typing import Dict, List +import dotenv +import prompts +import requests +import typings +import utilities from prompt_toolkit import PromptSession from prompt_toolkit.history import InMemoryHistory -import utilities -import typings -import prompts -from typing import List, Dict LOGIN_HEADERS = { "accept": "application/json", @@ -88,7 +88,12 @@ def authenticate(self): self.token = self.session.get(url, headers=headers).json() def ask( - self, system_prompt: str, prompt: str, code: str, language: str = "", model: str = "gpt-4" + self, + system_prompt: str, + prompt: str, + code: str, + language: str = "", + model: str = "gpt-4", ): if not self.token: self.authenticate() @@ -116,8 +121,11 @@ def ask( 400: "The developer of this plugin has made a mistake. Please report this issue.", 419: "You have been rate limited. Please try again later.", } - raise Exception(error_messages.get(response.status_code, - f"Unknown error: {response.status_code}")) + raise Exception( + error_messages.get( + response.status_code, f"Unknown error: {response.status_code}" + ) + ) for line in response.iter_lines(): line = line.decode("utf-8").replace("data: ", "").strip() if line.startswith("[DONE]"): @@ -150,7 +158,7 @@ def _get_embeddings(self, inputs: list[typings.FileExtract]): if i + 18 > len(inputs): data = utilities.generate_embedding_request(inputs[i:]) else: - data = utilities.generate_embedding_request(inputs[i: i + 18]) + data = utilities.generate_embedding_request(inputs[i : i + 18]) response = self.session.post(url, headers=self._headers(), json=data).json() if "data" not in response: raise Exception(f"Error fetching embeddings: {response}") diff --git a/rplugin/python3/handlers/vsplit_chat_handler.py b/rplugin/python3/handlers/vsplit_chat_handler.py index 7455d8e7..191fd6b2 100644 --- a/rplugin/python3/handlers/vsplit_chat_handler.py +++ b/rplugin/python3/handlers/vsplit_chat_handler.py @@ -1,6 +1,6 @@ -from mypynvim.core.nvim import MyNvim -from mypynvim.core.buffer import MyBuffer from handlers.chat_handler import ChatHandler +from mypynvim.core.buffer import MyBuffer +from mypynvim.core.nvim import MyNvim class VSplitChatHandler(ChatHandler): @@ -21,7 +21,7 @@ def vsplit(self): if window.vars[var_key]: self.nvim.current.window = window return - except: + except Exception: pass self.buffer.vsplit( diff --git a/rplugin/python3/mypynvim/core/autocmdmapper.py b/rplugin/python3/mypynvim/core/autocmdmapper.py index 8b604c14..7ba731b8 100644 --- a/rplugin/python3/mypynvim/core/autocmdmapper.py +++ b/rplugin/python3/mypynvim/core/autocmdmapper.py @@ -1,4 +1,5 @@ from __future__ import annotations + from typing import TYPE_CHECKING, Callable, Union if TYPE_CHECKING: diff --git a/rplugin/python3/mypynvim/core/keymapper.py b/rplugin/python3/mypynvim/core/keymapper.py index 6bc1d635..4c93ee6c 100644 --- a/rplugin/python3/mypynvim/core/keymapper.py +++ b/rplugin/python3/mypynvim/core/keymapper.py @@ -1,5 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Union, Callable + +from typing import TYPE_CHECKING, Callable, Union if TYPE_CHECKING: from .nvim import MyNvim diff --git a/rplugin/python3/mypynvim/core/nvim.py b/rplugin/python3/mypynvim/core/nvim.py index 773e10c8..4eeba15d 100644 --- a/rplugin/python3/mypynvim/core/nvim.py +++ b/rplugin/python3/mypynvim/core/nvim.py @@ -3,10 +3,10 @@ from pynvim import Nvim from pynvim.api.nvim import Current -from .window import MyWindow +from .autocmdmapper import AutocmdMapper from .buffer import MyBuffer from .keymapper import Keymapper -from .autocmdmapper import AutocmdMapper +from .window import MyWindow class MyNvim(Nvim): diff --git a/rplugin/python3/mypynvim/core/window.py b/rplugin/python3/mypynvim/core/window.py index 78ef0bf2..0aadfc4f 100644 --- a/rplugin/python3/mypynvim/core/window.py +++ b/rplugin/python3/mypynvim/core/window.py @@ -1,7 +1,9 @@ from __future__ import annotations + from typing import TYPE_CHECKING from pynvim.api import Window + from .buffer import MyBuffer if TYPE_CHECKING: diff --git a/rplugin/python3/mypynvim/ui_components/calculator.py b/rplugin/python3/mypynvim/ui_components/calculator.py index 307c7ab5..1f19d4ab 100644 --- a/rplugin/python3/mypynvim/ui_components/calculator.py +++ b/rplugin/python3/mypynvim/ui_components/calculator.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Literal, Union + from dataclasses import dataclass +from typing import TYPE_CHECKING, Literal, Union from mypynvim.core.nvim import MyNvim diff --git a/rplugin/python3/mypynvim/ui_components/layout.py b/rplugin/python3/mypynvim/ui_components/layout.py index 5862b0bc..dfea8c87 100644 --- a/rplugin/python3/mypynvim/ui_components/layout.py +++ b/rplugin/python3/mypynvim/ui_components/layout.py @@ -1,10 +1,11 @@ -from typing import Callable, Optional, Union, Literal, cast from dataclasses import dataclass +from typing import Callable, Literal, Optional, Union, cast from mypynvim.core.nvim import MyNvim -from .popup import PopUp + from .calculator import Calculator -from .types import Relative, PopUpConfiguration +from .popup import PopUp +from .types import PopUpConfiguration, Relative class Box: diff --git a/rplugin/python3/mypynvim/ui_components/popup.py b/rplugin/python3/mypynvim/ui_components/popup.py index 39ce8ed0..9040b452 100644 --- a/rplugin/python3/mypynvim/ui_components/popup.py +++ b/rplugin/python3/mypynvim/ui_components/popup.py @@ -1,19 +1,20 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Unpack, Union -from dataclasses import dataclass + from copy import deepcopy +from dataclasses import dataclass +from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union, Unpack if TYPE_CHECKING: from mypynvim.core.nvim import MyNvim + from .layout import Layout -if TYPE_CHECKING: - from mypynvim.core.nvim import MyNvim from mypynvim.core.buffer import MyBuffer from mypynvim.core.window import MyWindow + from .calculator import Calculator -from .types import Relative, PopUpConfiguration, PopUpArgs, PaddingKeys +from .types import PaddingKeys, PopUpArgs, PopUpConfiguration, Relative @dataclass diff --git a/rplugin/python3/mypynvim/ui_components/types.py b/rplugin/python3/mypynvim/ui_components/types.py index 64732ad7..a8d2ac39 100644 --- a/rplugin/python3/mypynvim/ui_components/types.py +++ b/rplugin/python3/mypynvim/ui_components/types.py @@ -1,5 +1,5 @@ -from typing import Literal, Union, TypedDict, Dict from dataclasses import dataclass +from typing import Dict, Literal, TypedDict, Union Relative = Literal["editor", "win", "cursor"] PaddingKeys = Dict[Literal["top", "right", "bottom", "left"], int] diff --git a/rplugin/python3/utilities.py b/rplugin/python3/utilities.py index 6aefd1e1..bc0540a7 100644 --- a/rplugin/python3/utilities.py +++ b/rplugin/python3/utilities.py @@ -1,8 +1,9 @@ +import json +import os +import random + import prompts import typings -import random -import os -import json def random_hex(length: int = 65):