Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions docs/source/development/change_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Fixes
.....

- 🐛 Replace absolute path with relative path to fix ``local-url`` not working on the non-local environment
- 🐛 Add more file extensions of C/C++ for SourceDiscover

.. _`release:1.0.0`:

Expand Down
19 changes: 1 addition & 18 deletions src/sphinx_codelinks/analyse/oneline_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
from enum import Enum
import logging

from sphinx_codelinks.config import (
ESCAPE,
SUPPORTED_COMMENT_TYPES,
UNIX_NEWLINE,
OneLineCommentStyle,
)
from sphinx_codelinks.config import ESCAPE, UNIX_NEWLINE, OneLineCommentStyle

# initialize logger
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -204,15 +199,3 @@ def is_newline_in_field(field: str) -> bool:
Check if the field contains a new line character.
"""
return UNIX_NEWLINE in field


def get_file_types(comment_type: str) -> list[str] | None:
"""
Get the list of file types to be discovered.
"""
file_types = (
list(SUPPORTED_COMMENT_TYPES)
if comment_type in SUPPORTED_COMMENT_TYPES
else None
)
return file_types
13 changes: 6 additions & 7 deletions src/sphinx_codelinks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

COMMENT_MARKERS = {CommentType.cpp: ["//", "/*"], CommentType.python: ["#"]}
ESCAPE = "\\"
SUPPORTED_COMMENT_TYPES = {"c", "h", "cpp", "hpp", "py"}


class CommentCategory(str, Enum):
Expand All @@ -47,7 +46,7 @@ def field_names(cls) -> set[str]:
@classmethod
def get_schema(cls, name: str) -> dict[str, Any] | None: # type: ignore[explicit-any]
_field = next(_field for _field in fields(cls) if _field.name is name)
if _field.metadata is not MISSING and "schema" in _field.metadata:
if _field.metadata and "schema" in _field.metadata:
return cast(dict[str, Any], _field.metadata["schema"]) # type: ignore[explicit-any]
return None

Expand Down Expand Up @@ -86,7 +85,7 @@ def field_names(cls) -> set[str]:
@classmethod
def get_schema(cls, name: str) -> dict[str, Any] | None: # type: ignore[explicit-any]
_field = next(_field for _field in fields(cls) if _field.name is name)
if _field.metadata is not MISSING and "schema" in _field.metadata:
if _field.metadata and "schema" in _field.metadata:
return cast(dict[str, Any], _field.metadata["schema"]) # type: ignore[explicit-any]
return None

Expand Down Expand Up @@ -219,14 +218,14 @@ def apply_needs_field_default(cls, given_fields: list[FieldConfig]) -> None:
@classmethod
def get_required_fields(cls, name: str) -> list[str] | None:
_field = next(_field for _field in fields(cls) if _field.name is name)
if _field.metadata is not MISSING:
if _field.metadata:
return cast(list[str], _field.metadata["required_fields"])
return None

@classmethod
def get_schema(cls, name: str) -> dict[str, Any] | None: # type: ignore[explicit-any]
_field = next(_field for _field in fields(cls) if _field.name is name)
if _field.metadata is not MISSING and "schema" in _field.metadata:
if _field.metadata and "schema" in _field.metadata:
return cast(dict[str, Any], _field.metadata["schema"]) # type: ignore[explicit-any]
return None

Expand Down Expand Up @@ -370,7 +369,7 @@ def field_names(cls) -> set[str]:
@classmethod
def get_schema(cls, name: str) -> dict[str, Any] | None: # type: ignore[explicit-any]
_field = next(_field for _field in fields(cls) if _field.name is name)
if _field.metadata is not MISSING and "schema" in _field.metadata:
if _field.metadata and "schema" in _field.metadata:
return cast(dict[str, Any], _field.metadata["schema"]) # type: ignore[explicit-any]
return None

Expand Down Expand Up @@ -566,7 +565,7 @@ def field_names(cls) -> set[str]:
def get_schema(cls, name: str) -> dict[str, Any] | None: # type: ignore[explicit-any]
"""Get the schema for a config item."""
_field = next(field for field in fields(cls) if field.name is name)
if _field.metadata is not MISSING and "schema" in _field.metadata:
if _field.metadata and "schema" in _field.metadata:
return _field.metadata["schema"] # type: ignore[no-any-return]
return None

Expand Down
8 changes: 4 additions & 4 deletions src/sphinx_codelinks/needextend_write.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Covert the generated JSON file created by CodeLinks anaylse to need-extend in RST."""
"""Convert the generated JSON file created by CodeLinks analyse to need-extend in RST."""

from collections import deque
from dataclasses import MISSING, dataclass, field, fields
from dataclasses import dataclass, field, fields
from os import linesep
from string import Template
from typing import Any, TypedDict, cast
Expand Down Expand Up @@ -105,7 +105,7 @@ def field_names(cls) -> set[str]:
@classmethod
def get_schema(cls, name: str) -> dict[str, Any] | None: # type: ignore[explicit-any]
_field = next(_field for _field in fields(cls) if _field.name is name)
if _field.metadata is not MISSING and "schema" in _field.metadata:
if _field.metadata and "schema" in _field.metadata:
return cast(dict[str, Any], _field.metadata["schema"]) # type: ignore[explicit-any]
return None

Expand Down Expand Up @@ -165,7 +165,7 @@ def convert_marked_content(
remote_url_field: str = "remote-url",
title: str | None = None,
) -> tuple[list[str], list[str]]:
"""Convert marked objects extracted by anaylse CLI to needextend in RST"""
"""Convert marked objects extracted by analyse CLI to needextend in RST"""
errors = []
needextend_texts: list[str] = []
intersted_objs = [
Expand Down
11 changes: 8 additions & 3 deletions src/sphinx_codelinks/source_discover/config.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from dataclasses import MISSING, dataclass, field, fields
from dataclasses import dataclass, field, fields
from enum import Enum
from pathlib import Path
from typing import Any, Required, TypedDict, cast

from jsonschema import ValidationError, validate

COMMENT_FILETYPE = {"cpp": ["c", "cpp", "h", "hpp"], "python": ["py"]}
# TODO: COMMENT_FILETYPE is probably not a good name, as C# uses the same comment style as C++, but it's grammatically different.
# Therefore, C# requires a different parser of tree-sitter to analyse its AST
COMMENT_FILETYPE = {
"cpp": ["c", "ci", "cpp", "cc", "cxx", "h", "hpp", "hxx", "hh", "ihl"],
"python": ["py"],
}


class CommentType(str, Enum):
Expand Down Expand Up @@ -73,7 +78,7 @@ def field_names(cls) -> set[str]:
@classmethod
def get_schema(cls, name: str) -> dict[str, Any] | None: # type: ignore[explicit-any]
_field = next(_field for _field in fields(cls) if _field.name is name)
if _field.metadata is not MISSING and "schema" in _field.metadata:
if _field.metadata and "schema" in _field.metadata:
return cast(dict[str, Any], _field.metadata["schema"]) # type: ignore[explicit-any]
return None

Expand Down
28 changes: 28 additions & 0 deletions tests/test_source_discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from sphinx_codelinks.source_discover.config import (
COMMENT_FILETYPE,
SourceDiscoverConfig,
SourceDiscoverConfigType,
)
Expand Down Expand Up @@ -145,3 +146,30 @@ def test_source_discover(
assert len(source_discover.source_paths) == num_files
if suffix:
assert all(path.suffix == ".cpp" for path in source_discover.source_paths)


@pytest.fixture(scope="function")
def create_source_files(tmp_path: Path) -> Path:
for file_types in COMMENT_FILETYPE.values():
for ext in file_types:
(tmp_path / f"file.{ext}").touch()
return tmp_path


@pytest.mark.parametrize(
("comment_type", "nums_files"),
[
("cpp", len(COMMENT_FILETYPE["cpp"])),
("python", len(COMMENT_FILETYPE["python"])),
],
)
def test_comment_filetype(
comment_type: str, nums_files: int, create_source_files: Path
) -> None:
src_dir = create_source_files

config = SourceDiscoverConfig(
src_dir=src_dir, comment_type=comment_type, gitignore=False
)
source_discover = SourceDiscover(config)
assert len(source_discover.source_paths) == nums_files
Loading