jarbin-c-coding-style 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jarbin_c_coding_style-2.0.0.dist-info/METADATA +150 -0
- jarbin_c_coding_style-2.0.0.dist-info/RECORD +186 -0
- jarbin_c_coding_style-2.0.0.dist-info/WHEEL +4 -0
- jarbin_c_coding_style-2.0.0.dist-info/entry_points.txt +2 -0
- jarbin_c_coding_style-2.0.0.dist-info/licenses/LICENSE +674 -0
- sources/__init__.py +0 -0
- sources/__main__.py +6 -0
- sources/analysis/__init__.py +0 -0
- sources/analysis/directives.py +258 -0
- sources/analysis/lexer.py +288 -0
- sources/analysis/parser.py +1754 -0
- sources/analysis/project.py +286 -0
- sources/analysis/source.py +326 -0
- sources/analysis/syntax.py +238 -0
- sources/analysis/tokens.py +116 -0
- sources/analysis/unit.py +700 -0
- sources/application.py +118 -0
- sources/build_info.json +6 -0
- sources/cli/__init__.py +0 -0
- sources/cli/arguments.py +507 -0
- sources/cli/completion.py +610 -0
- sources/cli/context.py +258 -0
- sources/cli/converters.py +204 -0
- sources/cli/help.py +162 -0
- sources/cli/parser.py +659 -0
- sources/configs/__init__.py +0 -0
- sources/configs/editor.py +207 -0
- sources/configs/ini.py +327 -0
- sources/configs/project.py +735 -0
- sources/configs/schema/__init__.py +0 -0
- sources/configs/schema/project.py +68 -0
- sources/constants.py +801 -0
- sources/diagnostics/__init__.py +0 -0
- sources/diagnostics/fingerprint.py +82 -0
- sources/diagnostics/model.py +146 -0
- sources/engine/__init__.py +0 -0
- sources/engine/api.py +355 -0
- sources/engine/examples.py +157 -0
- sources/engine/outcome.py +114 -0
- sources/engine/plan.py +440 -0
- sources/engine/runner.py +345 -0
- sources/engine/scanner.py +179 -0
- sources/enums.py +838 -0
- sources/errors/__init__.py +0 -0
- sources/errors/exceptions.py +727 -0
- sources/fixers/__init__.py +0 -0
- sources/fixers/base.py +209 -0
- sources/fixers/conflicts.py +129 -0
- sources/fixers/diff.py +73 -0
- sources/fixers/engine.py +434 -0
- sources/fixers/journal.py +290 -0
- sources/fixers/kinds.py +30 -0
- sources/fixers/layout.py +411 -0
- sources/fixers/model.py +151 -0
- sources/fixers/registry.py +54 -0
- sources/fixers/text.py +353 -0
- sources/fixers/verify.py +123 -0
- sources/fixers/writer.py +88 -0
- sources/integrations/__init__.py +0 -0
- sources/integrations/ci/__init__.py +0 -0
- sources/integrations/ci/github.py +183 -0
- sources/integrations/editors/__init__.py +0 -0
- sources/integrations/git/__init__.py +0 -0
- sources/integrations/git/repository.py +176 -0
- sources/integrations/update/__init__.py +0 -0
- sources/integrations/update/installers.py +529 -0
- sources/integrations/update/releases.py +397 -0
- sources/integrations/update/state.py +143 -0
- sources/outputs/__init__.py +0 -0
- sources/outputs/github/__init__.py +0 -0
- sources/outputs/github/renderer.py +135 -0
- sources/outputs/html/__init__.py +0 -0
- sources/outputs/html/renderer.py +694 -0
- sources/outputs/json/__init__.py +0 -0
- sources/outputs/json/renderer.py +154 -0
- sources/outputs/json/report.schema.json +121 -0
- sources/outputs/junit/__init__.py +0 -0
- sources/outputs/junit/renderer.py +151 -0
- sources/outputs/registry.py +135 -0
- sources/outputs/report.py +224 -0
- sources/outputs/sarif/__init__.py +0 -0
- sources/outputs/sarif/renderer.py +182 -0
- sources/outputs/terminal/__init__.py +0 -0
- sources/outputs/terminal/renderer.py +184 -0
- sources/outputs/terminal/style.py +159 -0
- sources/rules/__init__.py +0 -0
- sources/rules/arguments.py +362 -0
- sources/rules/catalog.py +227 -0
- sources/rules/categories/advanced/constant_pointers.ini +46 -0
- sources/rules/categories/advanced/line_break_at_end_of_file.ini +44 -0
- sources/rules/categories/advanced/static.ini +65 -0
- sources/rules/categories/advanced/typing.ini +47 -0
- sources/rules/categories/control_structures/conditional_branching.ini +89 -0
- sources/rules/categories/control_structures/goto.ini +45 -0
- sources/rules/categories/control_structures/ternary_operators.ini +57 -0
- sources/rules/categories/extensions/language_extensions.ini +71 -0
- sources/rules/categories/files_organization/contents_of_the_repository.ini +85 -0
- sources/rules/categories/files_organization/file_coherence.ini +166 -0
- sources/rules/categories/files_organization/file_extension.ini +68 -0
- sources/rules/categories/files_organization/fixtures/program +0 -0
- sources/rules/categories/files_organization/naming_files_and_folders.ini +74 -0
- sources/rules/categories/functions/coherence_of_functions.ini +45 -0
- sources/rules/categories/functions/comments_inside_a_function.ini +45 -0
- sources/rules/categories/functions/functions_without_parameters.ini +39 -0
- sources/rules/categories/functions/naming_functions.ini +79 -0
- sources/rules/categories/functions/number_of_columns.ini +61 -0
- sources/rules/categories/functions/number_of_lines.ini +70 -0
- sources/rules/categories/functions/number_of_parameters.ini +64 -0
- sources/rules/categories/functions/structures_as_parameters.ini +44 -0
- sources/rules/categories/global_scope/constant_values.ini +49 -0
- sources/rules/categories/global_scope/file_header.ini +92 -0
- sources/rules/categories/global_scope/global_variables.ini +53 -0
- sources/rules/categories/global_scope/include.ini +39 -0
- sources/rules/categories/global_scope/indentation_of_preprocessor.ini +62 -0
- sources/rules/categories/global_scope/inline_assembly.ini +43 -0
- sources/rules/categories/global_scope/leading_trailing_lines.ini +58 -0
- sources/rules/categories/global_scope/line_endings.ini +53 -0
- sources/rules/categories/global_scope/separation_of_functions.ini +61 -0
- sources/rules/categories/global_scope/trailing_spaces.ini +45 -0
- sources/rules/categories/header_files/content.ini +104 -0
- sources/rules/categories/header_files/include_guard.ini +47 -0
- sources/rules/categories/header_files/macros.ini +57 -0
- sources/rules/categories/layout_inside_a_function_scope/blank_lines.ini +71 -0
- sources/rules/categories/layout_inside_a_function_scope/code_line_content.ini +112 -0
- sources/rules/categories/layout_inside_a_function_scope/curly_brackets.ini +94 -0
- sources/rules/categories/layout_inside_a_function_scope/indentation.ini +96 -0
- sources/rules/categories/layout_inside_a_function_scope/spaces.ini +113 -0
- sources/rules/categories/layout_inside_a_function_scope/variable_declarations.ini +65 -0
- sources/rules/categories/variables_and_types/naming_identifiers.ini +74 -0
- sources/rules/categories/variables_and_types/pointers.ini +67 -0
- sources/rules/categories/variables_and_types/structures.ini +47 -0
- sources/rules/data/verbs.txt +182 -0
- sources/rules/definition.py +430 -0
- sources/rules/loader.py +925 -0
- sources/rules/paths.py +6 -0
- sources/rules/primitives/__init__.py +0 -0
- sources/rules/primitives/base.py +297 -0
- sources/rules/primitives/braces.py +211 -0
- sources/rules/primitives/directives.py +203 -0
- sources/rules/primitives/indentation.py +376 -0
- sources/rules/primitives/layout.py +199 -0
- sources/rules/primitives/naming.py +277 -0
- sources/rules/primitives/project.py +62 -0
- sources/rules/primitives/registry.py +49 -0
- sources/rules/primitives/repository.py +129 -0
- sources/rules/primitives/review.py +87 -0
- sources/rules/primitives/semantic.py +319 -0
- sources/rules/primitives/spacing.py +210 -0
- sources/rules/primitives/structure.py +586 -0
- sources/rules/primitives/syntax.py +200 -0
- sources/rules/primitives/text.py +408 -0
- sources/rules/primitives/tokens.py +84 -0
- sources/rules/template.py +138 -0
- sources/selftest/__init__.py +0 -0
- sources/selftest/cases.py +292 -0
- sources/services.py +90 -0
- sources/subcommands/__init__.py +0 -0
- sources/subcommands/base.py +45 -0
- sources/subcommands/check/__init__.py +0 -0
- sources/subcommands/check/command.py +45 -0
- sources/subcommands/check/fixing.py +224 -0
- sources/subcommands/check/handler.py +757 -0
- sources/subcommands/info/__init__.py +0 -0
- sources/subcommands/info/command.py +45 -0
- sources/subcommands/info/handler.py +527 -0
- sources/subcommands/registry.py +37 -0
- sources/subcommands/rule/__init__.py +0 -0
- sources/subcommands/rule/command.py +45 -0
- sources/subcommands/rule/handler.py +1067 -0
- sources/subcommands/rule/render.py +273 -0
- sources/subcommands/runner.py +124 -0
- sources/subcommands/test/__init__.py +0 -0
- sources/subcommands/test/command.py +45 -0
- sources/subcommands/test/handler.py +166 -0
- sources/subcommands/test/render.py +219 -0
- sources/subcommands/test/runner.py +430 -0
- sources/subcommands/update/__init__.py +0 -0
- sources/subcommands/update/command.py +45 -0
- sources/subcommands/update/handler.py +408 -0
- sources/utils/__init__.py +0 -0
- sources/utils/build_info.py +188 -0
- sources/utils/git.py +73 -0
- sources/utils/globs.py +98 -0
- sources/utils/logging.py +154 -0
- sources/utils/schema.py +77 -0
- sources/utils/versions.py +87 -0
|
File without changes
|
sources/fixers/base.py
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
from sources.analysis.source import Line
|
|
4
|
+
from sources.analysis.tokens import Token
|
|
5
|
+
from sources.analysis.unit import CUnit
|
|
6
|
+
from sources.enums import TokenKind
|
|
7
|
+
from sources.fixers.model import Edit, FixContext
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# Spaces per indentation level, and columns between two tab stops (rule decision: tab width of 4 columns).
|
|
11
|
+
INDENT_WIDTH: int = 4
|
|
12
|
+
|
|
13
|
+
# Token kinds that end a physical line.
|
|
14
|
+
BREAK_KINDS: frozenset[TokenKind] = frozenset({TokenKind.NEWLINE, TokenKind.LINE_SPLICE})
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Fixer(ABC):
|
|
18
|
+
"""
|
|
19
|
+
Turns one diagnostic into edits (plan 07). Fixers are pure: they read the file and return edits, never
|
|
20
|
+
write. Returning no edit means the diagnostic cannot be fixed mechanically (e.g. a comment in the way).
|
|
21
|
+
(ABC)
|
|
22
|
+
|
|
23
|
+
Attributes
|
|
24
|
+
----------
|
|
25
|
+
kind : str
|
|
26
|
+
Name used in `fix.kind`.
|
|
27
|
+
description : str
|
|
28
|
+
What the fix does (shown in reports and diffs).
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
kind: str = ""
|
|
32
|
+
description: str = ""
|
|
33
|
+
|
|
34
|
+
@abstractmethod
|
|
35
|
+
def propose(self, context: FixContext) -> tuple[Edit, ...]:
|
|
36
|
+
"""
|
|
37
|
+
Compute the edits resolving the diagnostic of the context.
|
|
38
|
+
|
|
39
|
+
Parameters
|
|
40
|
+
----------
|
|
41
|
+
context : FixContext
|
|
42
|
+
File, diagnostic and check parameters.
|
|
43
|
+
|
|
44
|
+
Returns
|
|
45
|
+
----------
|
|
46
|
+
tuple[Edit, ...]
|
|
47
|
+
The edits (empty when the diagnostic cannot be fixed).
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def leading_whitespace(line: Line) -> str:
|
|
52
|
+
"""
|
|
53
|
+
Leading spaces and tabs of a line.
|
|
54
|
+
|
|
55
|
+
Parameters
|
|
56
|
+
----------
|
|
57
|
+
line : Line
|
|
58
|
+
The line.
|
|
59
|
+
|
|
60
|
+
Returns
|
|
61
|
+
----------
|
|
62
|
+
str
|
|
63
|
+
The whitespace.
|
|
64
|
+
"""
|
|
65
|
+
return line.text[:len(line.text) - len(line.text.lstrip(" \t"))]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def delete_line(line: Line) -> Edit:
|
|
69
|
+
"""
|
|
70
|
+
Edit removing a whole line with its end of line.
|
|
71
|
+
|
|
72
|
+
Parameters
|
|
73
|
+
----------
|
|
74
|
+
line : Line
|
|
75
|
+
The line.
|
|
76
|
+
|
|
77
|
+
Returns
|
|
78
|
+
----------
|
|
79
|
+
Edit
|
|
80
|
+
The edit.
|
|
81
|
+
"""
|
|
82
|
+
return Edit(line.start, line.end + len(line.eol), "")
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def token_at(unit: CUnit, line: int, column: int, *, whitespace: bool = False) -> Token | None:
|
|
86
|
+
"""
|
|
87
|
+
Token starting at a position (the position of a diagnostic).
|
|
88
|
+
|
|
89
|
+
Parameters
|
|
90
|
+
----------
|
|
91
|
+
unit : CUnit
|
|
92
|
+
The file.
|
|
93
|
+
line : int
|
|
94
|
+
1-based line.
|
|
95
|
+
column : int
|
|
96
|
+
1-based column.
|
|
97
|
+
whitespace : bool
|
|
98
|
+
Whether a whitespace token is looked for (otherwise whitespace is skipped).
|
|
99
|
+
|
|
100
|
+
Returns
|
|
101
|
+
----------
|
|
102
|
+
Token | None
|
|
103
|
+
The token, or None.
|
|
104
|
+
"""
|
|
105
|
+
for token in unit.tokens_on_line(line):
|
|
106
|
+
if token.column == column and (token.kind is TokenKind.WHITESPACE) == whitespace:
|
|
107
|
+
return token
|
|
108
|
+
return None
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def token_end(token: Token) -> int:
|
|
112
|
+
"""
|
|
113
|
+
Offset just after a token.
|
|
114
|
+
|
|
115
|
+
Parameters
|
|
116
|
+
----------
|
|
117
|
+
token : Token
|
|
118
|
+
The token.
|
|
119
|
+
|
|
120
|
+
Returns
|
|
121
|
+
----------
|
|
122
|
+
int
|
|
123
|
+
The offset.
|
|
124
|
+
"""
|
|
125
|
+
return token.offset + len(token.text)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def set_gap_before(unit: CUnit, token: Token, wanted: str) -> Edit | None:
|
|
129
|
+
"""
|
|
130
|
+
Edit setting the whitespace between a token and the previous token of its line. Nothing is done at the
|
|
131
|
+
start of a line (that is indentation) or when the gap is already right.
|
|
132
|
+
|
|
133
|
+
Parameters
|
|
134
|
+
----------
|
|
135
|
+
unit : CUnit
|
|
136
|
+
The file.
|
|
137
|
+
token : Token
|
|
138
|
+
The token.
|
|
139
|
+
wanted : str
|
|
140
|
+
Whitespace wanted ("" to attach).
|
|
141
|
+
|
|
142
|
+
Returns
|
|
143
|
+
----------
|
|
144
|
+
Edit | None
|
|
145
|
+
The edit, or None when nothing can or must change.
|
|
146
|
+
"""
|
|
147
|
+
tokens: tuple[Token, ...] = unit.tokens
|
|
148
|
+
index: int = token.index - 1
|
|
149
|
+
if index < 0 or tokens[index].kind in BREAK_KINDS:
|
|
150
|
+
return None
|
|
151
|
+
previous: Token = tokens[index]
|
|
152
|
+
if previous.kind is TokenKind.WHITESPACE:
|
|
153
|
+
if index == 0 or tokens[index - 1].kind in BREAK_KINDS or previous.text == wanted:
|
|
154
|
+
return None
|
|
155
|
+
return Edit(previous.offset, token_end(previous), wanted)
|
|
156
|
+
return Edit(token.offset, token.offset, wanted) if wanted else None
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def set_gap_after(unit: CUnit, token: Token, wanted: str) -> Edit | None:
|
|
160
|
+
"""
|
|
161
|
+
Edit setting the whitespace between a token and the next token of its line. Nothing is done at the end
|
|
162
|
+
of a line, before a comment, or when the gap is already right.
|
|
163
|
+
|
|
164
|
+
Parameters
|
|
165
|
+
----------
|
|
166
|
+
unit : CUnit
|
|
167
|
+
The file.
|
|
168
|
+
token : Token
|
|
169
|
+
The token.
|
|
170
|
+
wanted : str
|
|
171
|
+
Whitespace wanted ("" to attach).
|
|
172
|
+
|
|
173
|
+
Returns
|
|
174
|
+
----------
|
|
175
|
+
Edit | None
|
|
176
|
+
The edit, or None when nothing can or must change.
|
|
177
|
+
"""
|
|
178
|
+
tokens: tuple[Token, ...] = unit.tokens
|
|
179
|
+
index: int = token.index + 1
|
|
180
|
+
if index >= len(tokens) or tokens[index].kind in BREAK_KINDS or tokens[index].kind is TokenKind.COMMENT:
|
|
181
|
+
return None
|
|
182
|
+
following: Token = tokens[index]
|
|
183
|
+
if following.kind is TokenKind.WHITESPACE:
|
|
184
|
+
after: Token | None = tokens[index + 1] if index + 1 < len(tokens) else None
|
|
185
|
+
if after is None or after.kind in BREAK_KINDS or after.kind is TokenKind.COMMENT or following.text == wanted:
|
|
186
|
+
return None
|
|
187
|
+
return Edit(following.offset, token_end(following), wanted)
|
|
188
|
+
return Edit(token_end(token), token_end(token), wanted) if wanted else None
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def only_layout_between(unit: CUnit, first: Token, last: Token) -> bool:
|
|
192
|
+
"""
|
|
193
|
+
Whether only whitespace and newlines lie strictly between two tokens (no comment, splice or directive).
|
|
194
|
+
|
|
195
|
+
Parameters
|
|
196
|
+
----------
|
|
197
|
+
unit : CUnit
|
|
198
|
+
The file.
|
|
199
|
+
first : Token
|
|
200
|
+
Earlier token.
|
|
201
|
+
last : Token
|
|
202
|
+
Later token.
|
|
203
|
+
|
|
204
|
+
Returns
|
|
205
|
+
----------
|
|
206
|
+
bool
|
|
207
|
+
True when the gap can be rewritten freely.
|
|
208
|
+
"""
|
|
209
|
+
return all(token.kind in (TokenKind.WHITESPACE, TokenKind.NEWLINE) for token in unit.tokens[first.index + 1:last.index])
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
from collections.abc import Iterable
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
|
|
4
|
+
from sources.enums import FixApplicability
|
|
5
|
+
from sources.fixers.model import Edit, Fix
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
# Priority of each applicability: safe fixes win over unsafe ones (plan 07, FIX-04).
|
|
9
|
+
APPLICABILITY_RANK: dict[FixApplicability, int] = {FixApplicability.SAFE: 0, FixApplicability.UNSAFE: 1}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True, slots=True)
|
|
13
|
+
class Resolution:
|
|
14
|
+
"""
|
|
15
|
+
Outcome of the conflict resolution of one file.
|
|
16
|
+
(dataclass)
|
|
17
|
+
|
|
18
|
+
Attributes
|
|
19
|
+
----------
|
|
20
|
+
kept : tuple[Fix, ...]
|
|
21
|
+
Fixes applied, in priority order.
|
|
22
|
+
edits : tuple[Edit, ...]
|
|
23
|
+
Their edits, de-duplicated, in application order.
|
|
24
|
+
dropped : tuple[Fix, ...]
|
|
25
|
+
Fixes left out because they overlap a fix of higher priority.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
kept: tuple[Fix, ...]
|
|
29
|
+
edits: tuple[Edit, ...]
|
|
30
|
+
dropped: tuple[Fix, ...]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def priority(fix: Fix) -> tuple[int, str, int, str, str]:
|
|
34
|
+
"""
|
|
35
|
+
Deterministic priority of a fix: applicability, then rule, then earliest offset (plan 07, FIX-04).
|
|
36
|
+
|
|
37
|
+
Parameters
|
|
38
|
+
----------
|
|
39
|
+
fix : Fix
|
|
40
|
+
The fix.
|
|
41
|
+
|
|
42
|
+
Returns
|
|
43
|
+
----------
|
|
44
|
+
tuple[int, str, int, str, str]
|
|
45
|
+
Sort key (smaller first).
|
|
46
|
+
"""
|
|
47
|
+
return (APPLICABILITY_RANK.get(fix.applicability, len(APPLICABILITY_RANK)), fix.rule_id, fix.start, fix.check_id or "", fix.kind)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def conflicts(edit: Edit, other: Edit) -> bool:
|
|
51
|
+
"""
|
|
52
|
+
Whether two edits cannot both be applied: overlapping ranges, or an insertion inside or at the edge of
|
|
53
|
+
a replaced range (ambiguous order). Two insertions at the same offset do not conflict.
|
|
54
|
+
|
|
55
|
+
Parameters
|
|
56
|
+
----------
|
|
57
|
+
edit : Edit
|
|
58
|
+
An edit.
|
|
59
|
+
other : Edit
|
|
60
|
+
Another edit.
|
|
61
|
+
|
|
62
|
+
Returns
|
|
63
|
+
----------
|
|
64
|
+
bool
|
|
65
|
+
True on conflict.
|
|
66
|
+
"""
|
|
67
|
+
if edit == other:
|
|
68
|
+
return False
|
|
69
|
+
if edit.start == edit.end and other.start == other.end:
|
|
70
|
+
return False
|
|
71
|
+
if edit.start == edit.end:
|
|
72
|
+
return other.start <= edit.start <= other.end
|
|
73
|
+
if other.start == other.end:
|
|
74
|
+
return edit.start <= other.start <= edit.end
|
|
75
|
+
return edit.start < other.end and other.start < edit.end
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def resolve_conflicts(fixes: Iterable[Fix]) -> Resolution:
|
|
79
|
+
"""
|
|
80
|
+
Choose the fixes of one file that can be applied together: by priority, a fix is kept when none of its
|
|
81
|
+
edits conflicts with an edit already kept; identical edits are merged; a dropped fix is dropped whole.
|
|
82
|
+
|
|
83
|
+
Parameters
|
|
84
|
+
----------
|
|
85
|
+
fixes : Iterable[Fix]
|
|
86
|
+
Fixes of one file.
|
|
87
|
+
|
|
88
|
+
Returns
|
|
89
|
+
----------
|
|
90
|
+
Resolution
|
|
91
|
+
Kept fixes, their edits in application order, and dropped fixes.
|
|
92
|
+
"""
|
|
93
|
+
kept: list[Fix] = []
|
|
94
|
+
dropped: list[Fix] = []
|
|
95
|
+
accepted: list[Edit] = []
|
|
96
|
+
for fix in sorted(fixes, key=priority):
|
|
97
|
+
if any(conflicts(edit, other) for edit in fix.edits for other in accepted):
|
|
98
|
+
dropped.append(fix)
|
|
99
|
+
continue
|
|
100
|
+
kept.append(fix)
|
|
101
|
+
accepted.extend(edit for edit in fix.edits if edit not in accepted)
|
|
102
|
+
order: dict[Edit, int] = {edit: position for position, edit in enumerate(accepted)}
|
|
103
|
+
return Resolution(tuple(kept), tuple(sorted(accepted, key=lambda edit: (edit.start, edit.end, order[edit]))), tuple(dropped))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def apply_edits(text: str, edits: Iterable[Edit]) -> str:
|
|
107
|
+
"""
|
|
108
|
+
Apply non-conflicting edits, given in application order (insertions at one offset keep their order).
|
|
109
|
+
|
|
110
|
+
Parameters
|
|
111
|
+
----------
|
|
112
|
+
text : str
|
|
113
|
+
Original text.
|
|
114
|
+
edits : Iterable[Edit]
|
|
115
|
+
The edits, sorted by start offset.
|
|
116
|
+
|
|
117
|
+
Returns
|
|
118
|
+
----------
|
|
119
|
+
str
|
|
120
|
+
The new text.
|
|
121
|
+
"""
|
|
122
|
+
parts: list[str] = []
|
|
123
|
+
position: int = 0
|
|
124
|
+
for edit in edits:
|
|
125
|
+
parts.append(text[position:edit.start])
|
|
126
|
+
parts.append(edit.replacement)
|
|
127
|
+
position = max(position, edit.end)
|
|
128
|
+
parts.append(text[position:])
|
|
129
|
+
return "".join(parts)
|
sources/fixers/diff.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import difflib
|
|
2
|
+
from collections.abc import Iterable
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
# Context lines around each change (plan 07, section 4).
|
|
6
|
+
CONTEXT_LINES: int = 3
|
|
7
|
+
|
|
8
|
+
# Marker git prints after a line that has no end of line.
|
|
9
|
+
NO_NEWLINE_MARKER: str = "\\n"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _lines(content: bytes) -> list[str]:
|
|
13
|
+
"""
|
|
14
|
+
Lines of a file, ends of line kept.
|
|
15
|
+
|
|
16
|
+
Parameters
|
|
17
|
+
----------
|
|
18
|
+
content : bytes
|
|
19
|
+
Content of the file.
|
|
20
|
+
|
|
21
|
+
Returns
|
|
22
|
+
----------
|
|
23
|
+
list[str]
|
|
24
|
+
The lines.
|
|
25
|
+
"""
|
|
26
|
+
return content.decode("utf-8", "surrogateescape").splitlines(keepends=True)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def unified_diff(path: str, before: bytes, after: bytes) -> str:
|
|
30
|
+
"""
|
|
31
|
+
Unified diff of one file (`a/<path>`, `b/<path>`, 3 context lines), git style for a missing final line
|
|
32
|
+
break.
|
|
33
|
+
|
|
34
|
+
Parameters
|
|
35
|
+
----------
|
|
36
|
+
path : str
|
|
37
|
+
File, relative POSIX path.
|
|
38
|
+
before : bytes
|
|
39
|
+
Original content.
|
|
40
|
+
after : bytes
|
|
41
|
+
Fixed content.
|
|
42
|
+
|
|
43
|
+
Returns
|
|
44
|
+
----------
|
|
45
|
+
str
|
|
46
|
+
The diff ("" when the contents are equal).
|
|
47
|
+
"""
|
|
48
|
+
parts: list[str] = []
|
|
49
|
+
for line in difflib.unified_diff(_lines(before), _lines(after), f"a/{path}", f"b/{path}", n=CONTEXT_LINES):
|
|
50
|
+
if line.startswith(("---", "+++")):
|
|
51
|
+
parts.append(line.rstrip("\r\n") + "\n")
|
|
52
|
+
elif line.endswith(("\n", "\r")):
|
|
53
|
+
parts.append(line)
|
|
54
|
+
else:
|
|
55
|
+
parts.append(line + "\n" + NO_NEWLINE_MARKER)
|
|
56
|
+
return "".join(parts)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def project_diff(changes: Iterable[tuple[str, bytes, bytes]]) -> str:
|
|
60
|
+
"""
|
|
61
|
+
Unified diff of several files, in path order (deterministic).
|
|
62
|
+
|
|
63
|
+
Parameters
|
|
64
|
+
----------
|
|
65
|
+
changes : Iterable[tuple[str, bytes, bytes]]
|
|
66
|
+
(path, original content, fixed content) of every file.
|
|
67
|
+
|
|
68
|
+
Returns
|
|
69
|
+
----------
|
|
70
|
+
str
|
|
71
|
+
The diff.
|
|
72
|
+
"""
|
|
73
|
+
return "".join(unified_diff(path, before, after) for path, before, after in sorted(changes, key=lambda change: change[0]))
|