-
Notifications
You must be signed in to change notification settings - Fork 53
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
Wrap signatures onto several lines when function len is over a treshold and function has the focus #831
Merged
Merged
Wrap signatures onto several lines when function len is over a treshold and function has the focus #831
Changes from 28 commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
dea121c
Introduce ParsedDocstring.with_linker()/.with_tag()/.combine(). The w…
tristanlatr 95f8f3d
Colorize the signature ourself.
tristanlatr 0b7c76e
Some more adjustments. Make the self param always inline. Try to opti…
tristanlatr 188c410
Add comment
tristanlatr 83d47f7
Fix usage of cache
tristanlatr ab1cdd1
Fix usages of cache
tristanlatr eca5ced
Simplify with_linker() and with_tag(). These do not create new parsed…
tristanlatr ff4269f
Revert "Simplify with_linker() and with_tag(). These do not create ne…
tristanlatr 914e01c
Minor changes not to use lru_cache too much
tristanlatr cdef965
Try to optimize what I can
tristanlatr 2033d65
Fix mypy
tristanlatr 5396396
Merge branch 'master' into 801-signature-spans
tristanlatr 282250b
Remove unused imports
tristanlatr 6a4de9f
Better implementation of with_linker and with_tag inside a single sub…
tristanlatr c0f93dc
First attempt to implement relatively smart Expand/Collapse signature…
tristanlatr da89d7c
Simplify things: don't try to wrap overload signatures. Sphinx doesn'…
tristanlatr 141b211
Get rid of the ParsedStanOnly by using parsed_text_with_css instead.
tristanlatr a46a3a3
Few simplifications here and there.
tristanlatr 40ac0a6
Use the CSS class 'decorator' for all decorators.
tristanlatr 4172485
Fix various bugs in the implementation.
tristanlatr 7103ce5
Fix pyflakes
tristanlatr eae961a
Fix format_undocumented_summary returning a tuple of strings instead …
tristanlatr 7c6c6eb
increase the threshold for a function to be rendered in several lines.
tristanlatr 19400ff
Avoid an empty div for decorators when there are no decorators.
tristanlatr a3ebbdf
Use non breaking spaces in sugnature defs.
tristanlatr cd257eb
Improve a little bit the rendering of parameter tables that uses very…
tristanlatr 907792a
Get rid of the AnnotationLinker - drop the verbose messages when an a…
tristanlatr 977e5b5
Merge branch 'master' into 801-signature-spans
tristanlatr 91edc51
Change comment
tristanlatr b504c21
Merge branch '801-signature-spans' of github.com:twisted/pydoctor int…
tristanlatr 25b5e62
Add an environment to build temporalio docs
tristanlatr bd2de92
Add a bug overload in the google demo
tristanlatr cc82f10
Apply suggestions from code review
tristanlatr 07fc41d
Merge branch 'master' into 801-signature-spans
tristanlatr 668f4d0
Fix the NotFoundLinker
tristanlatr 7fc2b10
Do not mark overloaded functions with css class .long-signature
tristanlatr 80de043
Remove unused imports
tristanlatr a9c5bf2
Add readme entries
tristanlatr 6784e4c
Upadate docs tests
tristanlatr 78f73b9
Like back, consider a function long from 88 chars.
tristanlatr bf7045f
Adjust test again
tristanlatr a37b028
Update README.rst
tristanlatr 8b24212
Merge branch 'master' into 801-signature-spans
tristanlatr 56168f8
Fix typo
tristanlatr 1f93800
Revert some CSS changes covered in #872
tristanlatr eecf39e
Remove comment
tristanlatr 95de622
Remove duplicated code
tristanlatr 6fa8dfe
css: make .rst-literal and <code> tags closely equivalent.
tristanlatr 5e946b7
Don't store a signature when it has invalid parameters
tristanlatr 4c8c9b5
Get rid of the methods ParsedDocstring.with_tag() and combine(). Repl…
tristanlatr 18773fe
Merge branch 'master' into 801-signature-spans
tristanlatr 2090127
Fix typr checking
tristanlatr b25905b
Merge branch '801-signature-spans' of github.com:twisted/pydoctor int…
tristanlatr 4bb1065
Adjust test
tristanlatr 05a7794
Apply suggestions from code review
tristanlatr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -33,7 +33,9 @@ | |||
from __future__ import annotations | ||||
__docformat__ = 'epytext en' | ||||
|
||||
from typing import Callable, ContextManager, List, Optional, Sequence, Iterator, TYPE_CHECKING | ||||
import contextlib | ||||
from itertools import chain | ||||
from typing import Callable, ContextManager, Iterable, List, Optional, Sequence, Iterator, TYPE_CHECKING | ||||
import abc | ||||
import sys | ||||
import re | ||||
|
@@ -129,7 +131,7 @@ | |||
markup parsers such as L{pydoctor.epydoc.markup.epytext.parse_docstring()} | ||||
or L{pydoctor.epydoc.markup.restructuredtext.parse_docstring()}. | ||||
|
||||
Subclasses must implement L{has_body()} and L{to_node()}. | ||||
Subclasses must at least implement L{has_body()} and L{to_node()}. | ||||
|
||||
A default implementation for L{to_stan()} method, relying on L{to_node()} is provided. | ||||
But some subclasses override this behaviour. | ||||
|
@@ -143,11 +145,10 @@ | |||
A list of L{Field}s, each of which encodes a single field. | ||||
The field's bodies are encoded as C{ParsedDocstring}s. | ||||
""" | ||||
|
||||
self._stan: Optional[Tag] = None | ||||
self._summary: Optional['ParsedDocstring'] = None | ||||
|
||||
@abc.abstractproperty | ||||
@property | ||||
@abc.abstractmethod | ||||
def has_body(self) -> bool: | ||||
""" | ||||
Does this docstring have a non-empty body? | ||||
|
@@ -168,7 +169,6 @@ | |||
docstring_toc = new_document('toc') | ||||
if contents: | ||||
docstring_toc.extend(contents) | ||||
from pydoctor.epydoc.markup.restructuredtext import ParsedRstDocstring | ||||
return ParsedRstDocstring(docstring_toc, ()) | ||||
else: | ||||
return None | ||||
|
@@ -203,25 +203,115 @@ | |||
""" | ||||
raise NotImplementedError() | ||||
|
||||
def to_text(self) -> str: | ||||
""" | ||||
Translate this docstring to a string. | ||||
The default implementation depends on L{to_node}. | ||||
""" | ||||
doc = self.to_node() | ||||
return ''.join(node2stan.gettext(doc)) | ||||
|
||||
def with_tag(self, tag: Tag) -> ParsedDocstring: | ||||
""" | ||||
Wraps the L{to_stan()} result inside the given tag. | ||||
|
||||
This is useful because some code strips the main tag to keep only it's content. | ||||
With this trick, the main tag is preserved. It can also be used to add | ||||
a custom CSS class on top of an existing parsed docstring. | ||||
""" | ||||
return _ParsedDocstringWithTag(self, tag) | ||||
|
||||
@classmethod | ||||
def combine(cls, elements: Sequence[ParsedDocstring]) -> ParsedDocstring: | ||||
""" | ||||
Combine the contents of several parsed docstrings into one. | ||||
""" | ||||
return _ParsedDocstringTree(elements) | ||||
|
||||
def get_summary(self) -> 'ParsedDocstring': | ||||
""" | ||||
Returns the summary of this docstring. | ||||
|
||||
@note: The summary is cached. | ||||
""" | ||||
# Avoid rare cyclic import error, see https://github.com/twisted/pydoctor/pull/538#discussion_r845668735 | ||||
from pydoctor import epydoc2stan | ||||
if self._summary is not None: | ||||
return self._summary | ||||
try: | ||||
_document = self.to_node() | ||||
visitor = SummaryExtractor(_document) | ||||
_document.walk(visitor) | ||||
except Exception: | ||||
self._summary = epydoc2stan.ParsedStanOnly(tags.span(class_='undocumented')("Broken summary")) | ||||
else: | ||||
self._summary = visitor.summary or epydoc2stan.ParsedStanOnly(tags.span(class_='undocumented')("No summary")) | ||||
return self._summary | ||||
return parsed_text_with_css('Broken summary', 'undocumented') | ||||
|
||||
return visitor.summary or parsed_text_with_css('No summary', 'undocumented') | ||||
|
||||
|
||||
class _ParsedDocstringTree(ParsedDocstring): | ||||
""" | ||||
Several parsed docstrings into a single one. | ||||
""" | ||||
|
||||
def __init__(self, elements: Sequence[ParsedDocstring]): | ||||
super().__init__(tuple(chain.from_iterable(e.fields for e in elements))) | ||||
self._elements = elements | ||||
self._doc: nodes.document | None = None | ||||
|
||||
@property | ||||
def has_body(self) -> bool: | ||||
return any(e.has_body for e in self._elements) | ||||
tristanlatr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
@classmethod | ||||
def _generate_document(cls, elements: Iterable[ParsedDocstring]) -> nodes.document: | ||||
doc = new_document('composite') | ||||
for e in elements: | ||||
# TODO: Some parsed doctrings simply do not implement to_node(). | ||||
# It should be really time to fix this... | ||||
subdoc = e.to_node() | ||||
# TODO: here all childrens might not have the same document property. | ||||
# this should not be a problem, but docutils is likely not meant to be used like that. | ||||
doc.children.extend(subdoc.children) | ||||
return doc | ||||
|
||||
def to_node(self) -> nodes.document: | ||||
if not self._doc: | ||||
self._doc = self._generate_document(self._elements) | ||||
return self._doc | ||||
|
||||
def to_stan(self, linker: DocstringLinker) -> Tag: | ||||
tristanlatr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
stan = tags.transparent() | ||||
for e in self._elements: | ||||
stan(e.to_stan(linker).children) | ||||
return stan | ||||
|
||||
class _ParsedDocstringWithTag(ParsedDocstring): | ||||
""" | ||||
Wraps a parsed docstring to wrap the result of the | ||||
the to_stan() method inside a custom Tag. | ||||
""" | ||||
def __init__(self, | ||||
other: ParsedDocstring, | ||||
tag: Tag): | ||||
super().__init__(other.fields) | ||||
self.wrapped = other | ||||
""" | ||||
The wrapped parsed docstring. | ||||
""" | ||||
self._tag = tag | ||||
self._stan: Tag | None = None | ||||
|
||||
# We double wrap it with a transparent tag so the added tags survives ParsedDocstring.combine | ||||
# wich combines the content of the main div of the stan, not the div itself. | ||||
def to_stan(self, linker: DocstringLinker) -> Tag: | ||||
# Since the stan is cached inside _stan attribute we can't simply use | ||||
# "lambda this, linker: tags.transparent(self._tag(this.to_stan(linker)))" as the new to_stan method. | ||||
# this would not behave correctly because each time to_stan will be called, the content would be duplicated. | ||||
if (stan:=self._stan) is not None: | ||||
return stan | ||||
self._stan = stan = Tag('')(self._tag(self.wrapped.to_stan(linker))) | ||||
return stan | ||||
|
||||
# Boring | ||||
def to_node(self) -> nodes.document: | ||||
return self.wrapped.to_node() | ||||
@property | ||||
def has_body(self) -> bool: | ||||
return self.wrapped.has_body | ||||
|
||||
|
||||
################################################## | ||||
|
@@ -286,14 +376,16 @@ | |||
target URL for crossreference links. | ||||
""" | ||||
|
||||
def link_to(self, target: str, label: "Flattenable") -> Tag: | ||||
def link_to(self, target: str, label: "Flattenable", *, is_annotation: bool = False) -> Tag: | ||||
""" | ||||
Format a link to a Python identifier. | ||||
This will resolve the identifier like Python itself would. | ||||
|
||||
@param target: The name of the Python identifier that | ||||
should be linked to. | ||||
@param label: The label to show for the link. | ||||
@param is_annotation: Generated links will give precedence to the module | ||||
defined varaible rather the nested definitions when there are name colisions. | ||||
tristanlatr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
@return: The link, or just the label if the target was not found. | ||||
""" | ||||
|
||||
|
@@ -325,6 +417,20 @@ | |||
in this case error will NOT be reported at all. | ||||
""" | ||||
|
||||
class NotFoundLinker(DocstringLinker): | ||||
tristanlatr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
"""A DocstringLinker implementation that cannot find any links.""" | ||||
|
||||
def link_to(self, target: str, label: "Flattenable", *, is_annotation: bool = False) -> Tag: | ||||
return tags.transparent(label) | ||||
|
||||
def link_xref(self, target: str, label: "Flattenable", lineno: int) -> Tag: | ||||
return tags.code(label) | ||||
|
||||
@contextlib.contextmanager | ||||
def switch_context(self, ob: Documentable | None) -> Iterator[None]: | ||||
yield | ||||
|
||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
################################################## | ||||
## ParseError exceptions | ||||
################################################## | ||||
|
@@ -475,11 +581,13 @@ | |||
set_node_attributes(nodes.paragraph('', ''), document=summary_doc, lineno=1, | ||||
children=summary_pieces)]) | ||||
|
||||
from pydoctor.epydoc.markup.restructuredtext import ParsedRstDocstring | ||||
self.summary = ParsedRstDocstring(summary_doc, fields=[]) | ||||
|
||||
def visit_field(self, node: nodes.Node) -> None: | ||||
raise nodes.SkipNode() | ||||
|
||||
def unknown_visit(self, node: nodes.Node) -> None: | ||||
'''Ignore all unknown nodes''' | ||||
|
||||
|
||||
from pydoctor.epydoc.markup.restructuredtext import ParsedRstDocstring, parsed_text_with_css |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be None and then we render it like (...) in the templatewriter