Skip to content
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

Fix Directive argument name and value ranges on non-first line #79

Merged
merged 8 commits into from
Feb 1, 2024

Conversation

Kyle-Ye
Copy link
Contributor

@Kyle-Ye Kyle-Ye commented Sep 25, 2022

Bug/issue #, if applicable:

Close #71

Summary

  • Remove unnecessary optional check
  • Fix and add test case for single line block parse

Dependencies

None

Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

  • Added tests
  • Ran the ./bin/test script and it succeeded
  • Updated documentation if necessary

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Sep 25, 2022

IMG_3185

Some rough draft images that may help code review

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Sep 26, 2022

@swift-ci please test

@@ -201,7 +201,8 @@ public struct DirectiveArgumentText: Equatable {
var line = TrimmedLine(untrimmedText[...],
source: range?.lowerBound.source,
lineNumber: range?.lowerBound.line,
parseIndex: parseIndex)
parseIndex: parseIndex,
startParseIndex: untrimmedText.index(parseIndex, offsetBy: 1 - (range?.lowerBound.column ?? 1)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code again, it seems like there's an existing lineStartIndex property on this LineSegment type that should always be populated. Did that not work for this purpose?

Copy link
Contributor Author

@Kyle-Ye Kyle-Ye Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/// Create an argument line segment.
/// - Parameters:
///   - untrimmedText: the segment's untrimmed text from which arguments can be parsed.
///   - lineStartIndex: the index in ``text`` where the line started.
means.

As the documentation says, lineStartIndex is the index in text where the line started.

And lineStartIndex is init as untrimmedText.startIndex in BlockDirectParser.

eg.

(lldb) po untrimmedText
"hello\n @Options(scope: page"

(lldb) po untrimmedText[lineStartIndex]
"h"

(lldb) po untrimmedText[untrimmedText.index(parseIndex, offsetBy: 1 - (range?.lowerBound.column ?? 1))]
" "

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking around the usage of lineStartIndex. It seems it's currently almost not used in any place.

@Kyle-Ye Kyle-Ye requested a review from QuietMisdreavus May 25, 2023 15:16
@Kyle-Ye Kyle-Ye requested a review from d-ronnqvist July 13, 2023 15:08
@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Jul 13, 2023

The bug has been there for some time. Can anyone help continue to review this so that we can merge and solve it? Thanks

@d-ronnqvist
Copy link
Contributor

I looked at this today and as far as I can tell the issue is that TrimmedLine and LineSegment mean different things by untrimmedText. For TrimmedLine the untrimmed text is only expecting to hold one line of text:

/// The original untrimmed text of the line.
let untrimmedText: Substring

whereas LineSegment holds a potentially larger string and points to the index where the line starts

/// The segment's untrimmed text from which arguments can be parsed.
public var untrimmedText: String
/// The index in ``untrimmedText`` where the line started.
public var lineStartIndex: String.Index

Because of this difference, passing the full untrimmed line from the line segment to the trimmed line is probably not the right thing to do.

I checked out this branch to continue investigating this issue and two things worth pointing out:

  • The code that creates a LineSegment for each directive argument text doesn't compute lineStartIndex, it just passes the start index of the full text.
  • The code that creates a LineSegment for each directive argument text does slice of the "untrimmed text" after the end index of the argument text. For every line of directive arguments, this creates a copy of the entire document up to and including that line.
  • The TrimmedLine only trims what comes before the parseIndex.

Since the start and end of the line segment's "untrimmedText" is sliced in two different places, it raises a question of which place should be responsible for slicing the line segment's text.

I think both of these should be done in one place, so I could imagine two different solutions here:

  • Change LineSegment so that untrimmedText only stores one line of text (same as TrimmedLine.untrimmedText). This means that lineStartIndex is always untrimmedText.startIndex.
  • Add API to LineSegment so that the line substring can be retrieved and use that so that TrimmedLine is initialized with only one line.

Looking at the other places that create a LineSegment I felt that the first alternative was the better solution, mainly because it makes both untrimmedText properties mean that same thing.

Since I had already done most of this locally at this point I opened a PR here with the changes I made.

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Dec 11, 2023

Thanks for the follow-up. I'll read and review it later this week.

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Dec 12, 2023

@swift-ci please test

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Dec 12, 2023

Do we need a 3rd people to help review this? @d-ronnqvist Maybe you can invite one

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Feb 1, 2024

@swift-ci please test

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Feb 1, 2024

@d-ronnqvist Would you help give this a final review so that we can merge it?

@Kyle-Ye Kyle-Ye merged commit 0b59ad6 into swiftlang:main Feb 1, 2024
2 checks passed
@Kyle-Ye Kyle-Ye deleted the bugfix/parser branch February 1, 2024 14:58
cgrindel-self-hosted-renovate bot referenced this pull request in cgrindel/rules_swift_package_manager Sep 20, 2024
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
|
[apple/swift-markdown](https://redirect.github.com/apple/swift-markdown)
| minor | `0.4.0` -> `0.5.0` |

---

### Release Notes

<details>
<summary>apple/swift-markdown (apple/swift-markdown)</summary>

###
[`v0.5.0`](https://redirect.github.com/swiftlang/swift-markdown/releases/tag/0.5.0):
Swift-Markdown 0.5.0

[Compare
Source](https://redirect.github.com/apple/swift-markdown/compare/0.4.0...0.5.0)

This release is based on the code that shipped into Swift 6.0.

#### What's Changed

- Add link title support for commonmark by
[@&#8203;Kyle-Ye](https://redirect.github.com/Kyle-Ye) in
[https://github.com/swiftlang/swift-markdown/pull/140](https://redirect.github.com/swiftlang/swift-markdown/pull/140)
- Remove dependency on argument-parser by
[@&#8203;ethan-kusters](https://redirect.github.com/ethan-kusters) in
[https://github.com/swiftlang/swift-markdown/pull/149](https://redirect.github.com/swiftlang/swift-markdown/pull/149)
- Fix utf8 decode by
[@&#8203;zunda-pixel](https://redirect.github.com/zunda-pixel) in
[https://github.com/swiftlang/swift-markdown/pull/145](https://redirect.github.com/swiftlang/swift-markdown/pull/145)
- Fix multi line symbol link source range issue by
[@&#8203;Kyle-Ye](https://redirect.github.com/Kyle-Ye) in
[https://github.com/swiftlang/swift-markdown/pull/151](https://redirect.github.com/swiftlang/swift-markdown/pull/151)
- Fix multiline directive without content parsing range issue by
[@&#8203;Kyle-Ye](https://redirect.github.com/Kyle-Ye) in
[https://github.com/swiftlang/swift-markdown/pull/154](https://redirect.github.com/swiftlang/swift-markdown/pull/154)
- build: add a CMake based build by
[@&#8203;compnerd](https://redirect.github.com/compnerd) in
[https://github.com/swiftlang/swift-markdown/pull/141](https://redirect.github.com/swiftlang/swift-markdown/pull/141)
- Add 2024 as an accepted year number by
[@&#8203;Kyle-Ye](https://redirect.github.com/Kyle-Ye) in
[https://github.com/swiftlang/swift-markdown/pull/160](https://redirect.github.com/swiftlang/swift-markdown/pull/160)
- Fix Directive argument name and value ranges on non-first line by
[@&#8203;Kyle-Ye](https://redirect.github.com/Kyle-Ye) in
[https://github.com/swiftlang/swift-markdown/pull/79](https://redirect.github.com/swiftlang/swift-markdown/pull/79)
- Add support for Doxygen discussion/note tags by
[@&#8203;j-f1](https://redirect.github.com/j-f1) in
[https://github.com/swiftlang/swift-markdown/pull/159](https://redirect.github.com/swiftlang/swift-markdown/pull/159)
- Add support for formatting the new Doxygen types using MarkupFormatter
by [@&#8203;j-f1](https://redirect.github.com/j-f1) in
[https://github.com/swiftlang/swift-markdown/pull/163](https://redirect.github.com/swiftlang/swift-markdown/pull/163)
- Support Windows ARM64 builds by
[@&#8203;hjyamauchi](https://redirect.github.com/hjyamauchi) in
[https://github.com/swiftlang/swift-markdown/pull/164](https://redirect.github.com/swiftlang/swift-markdown/pull/164)
- build: silence warning about CMakeLists.txt from SPM by
[@&#8203;compnerd](https://redirect.github.com/compnerd) in
[https://github.com/swiftlang/swift-markdown/pull/167](https://redirect.github.com/swiftlang/swift-markdown/pull/167)

#### New Contributors

- [@&#8203;zunda-pixel](https://redirect.github.com/zunda-pixel) made
their first contribution in
[https://github.com/swiftlang/swift-markdown/pull/145](https://redirect.github.com/swiftlang/swift-markdown/pull/145)
- [@&#8203;j-f1](https://redirect.github.com/j-f1) made their first
contribution in
[https://github.com/swiftlang/swift-markdown/pull/159](https://redirect.github.com/swiftlang/swift-markdown/pull/159)
- [@&#8203;hjyamauchi](https://redirect.github.com/hjyamauchi) made
their first contribution in
[https://github.com/swiftlang/swift-markdown/pull/164](https://redirect.github.com/swiftlang/swift-markdown/pull/164)

**Full Changelog**:
swiftlang/swift-markdown@0.4.0...0.5.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config
help](https://redirect.github.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://redirect.github.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC44OS4xIiwidXBkYXRlZEluVmVyIjoiMzguODkuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: cgrindel-self-hosted-renovate[bot] <139595543+cgrindel-self-hosted-renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Directive argument name and value ranges are incorrect for single-line directives
4 participants