Skip to content

Commit

Permalink
Add pre-commit for black, flake8, and prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
gptlang committed Feb 2, 2024
1 parent cc3a05c commit 3f74bb7
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 62 deletions.
26 changes: 6 additions & 20 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"files": [
"README.md"
],
"files": ["README.md"],
"imageSize": 100,
"commit": false,
"commitType": "docs",
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 88
ignore = E203, E501, W503
7 changes: 2 additions & 5 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
{
"diagnostics.globals": [
"describe",
"it"
]
}
"diagnostics.globals": ["describe", "it"]
}
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
{
Expand Down
4 changes: 1 addition & 3 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
]
"extends": ["config:recommended"]
}
4 changes: 2 additions & 2 deletions rplugin/python3/copilot-agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
4 changes: 3 additions & 1 deletion rplugin/python3/copilot-plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
32 changes: 20 additions & 12 deletions rplugin/python3/copilot.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]"):
Expand Down Expand Up @@ -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}")
Expand Down
6 changes: 3 additions & 3 deletions rplugin/python3/handlers/vsplit_chat_handler.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions rplugin/python3/mypynvim/core/autocmdmapper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Callable, Union

if TYPE_CHECKING:
Expand Down
3 changes: 2 additions & 1 deletion rplugin/python3/mypynvim/core/keymapper.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions rplugin/python3/mypynvim/core/nvim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions rplugin/python3/mypynvim/core/window.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
3 changes: 2 additions & 1 deletion rplugin/python3/mypynvim/ui_components/calculator.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 4 additions & 3 deletions rplugin/python3/mypynvim/ui_components/layout.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
11 changes: 6 additions & 5 deletions rplugin/python3/mypynvim/ui_components/popup.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion rplugin/python3/mypynvim/ui_components/types.py
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
7 changes: 4 additions & 3 deletions rplugin/python3/utilities.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit 3f74bb7

Please sign in to comment.