Skip to content

Commit

Permalink
fix: maxWidth of lines
Browse files Browse the repository at this point in the history
issue: #70
  • Loading branch information
hosseinmd committed Feb 12, 2021
1 parent 8f18200 commit 5f807ce
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 27 deletions.
8 changes: 5 additions & 3 deletions src/descriptionFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { format } from "prettier";
import { TAGS_NEED_FORMAT_DESCRIPTION } from "./roles";
import { DESCRIPTION, EXAMPLE, TODO } from "./tags";
import { Comment } from "comment-parser";
import { JsdocOptions } from "./types";
import { capitalizer } from "./utils";

Expand Down Expand Up @@ -219,10 +218,13 @@ function breakDescriptionToLines(
const extraLastLineWidth = 10;
let result = "";
while (str.length > maxWidth + extraLastLineWidth) {
let sliceIndex = str.lastIndexOf(" ", maxWidth);
let sliceIndex = str.lastIndexOf(
" ",
str.startsWith("\n") ? maxWidth + 1 : maxWidth,
);
/**
* When a str is a long word lastIndexOf will gives 4 every time loop
* running on limited time
* running unlimited time
*/
if (sliceIndex <= beginningSpace.length)
sliceIndex = str.indexOf(" ", beginningSpace.length + 1);
Expand Down
3 changes: 2 additions & 1 deletion src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ const stringify = (
} else {
// append the description to the tag
tagString += formatDescription(tag, description, options, {
firstLinePrintWidth: printWidth - tagString.length,
// 1 is `\n` which added to tagString
firstLinePrintWidth: printWidth - (tagString.length - 1),
});
}
}
Expand Down
16 changes: 8 additions & 8 deletions tests/__snapshots__/files/prism-core.js.shot
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ var Prism = (function (_self) {
* Returns whether a given class is active for \`element\`.
*
* The class can be activated if \`element\` or one of its ancestors has the given class and it can be deactivated
* if \`element\` or one of its ancestors has the negated version of the given class. The _negated version_ of
* the given class is just the given class with a \`no-\` prefix.
* if \`element\` or one of its ancestors has the negated version of the given class. The _negated version_ of the
* given class is just the given class with a \`no-\` prefix.
*
* Whether the class is active is determined by the closest ancestor of \`element\` (where \`element\` itself is
* closest ancestor) that has the given class or the negated version of it. If neither \`element\` nor any of its
Expand Down Expand Up @@ -270,8 +270,8 @@ var Prism = (function (_self) {
* ## Best practices
*
* Since the position of overwriting tokens (token in \`redef\` that overwrite tokens in the copied language)
* doesn't matter, they can technically be in any order. However, this can be confusing to others that trying
* to understand the language definition because, normally, the order of tokens matters in Prism grammars.
* doesn't matter, they can technically be in any order. However, this can be confusing to others that trying to
* understand the language definition because, normally, the order of tokens matters in Prism grammars.
*
* Therefore, it is encouraged to order overwriting tokens according to the positions of the overwritten tokens.
* Furthermore, all non-overwriting tokens should be placed after the overwriting ones.
Expand Down Expand Up @@ -587,8 +587,8 @@ var Prism = (function (_self) {
},

/**
* Low-level function, only use if you know what you’re doing. It accepts a string of text as input and the
* language definitions to use, and returns a string with the HTML produced.
* Low-level function, only use if you know what you’re doing. It accepts a string of text as input and the language
* definitions to use, and returns a string with the HTML produced.
*
* The following hooks will be run:
* 1. \`before-tokenize\`
Expand Down Expand Up @@ -620,8 +620,8 @@ var Prism = (function (_self) {
},

/**
* This is the heart of Prism, and the most low-level function you can use. It accepts a string of text as input
* and the language definitions to use, and returns an array with the tokenized code.
* This is the heart of Prism, and the most low-level function you can use. It accepts a string of text as input and
* the language definitions to use, and returns an array with the tokenized code.
*
* When the language definition includes nested tokens, the function is called recursively on each of these tokens.
*
Expand Down
16 changes: 8 additions & 8 deletions tests/__snapshots__/main.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ exports[`Long description memory leak 1`] = `
*
* @param {LogLevel | string | ILogger} logging A {@link
* @microsoft/signalr.LogLevel}, a string representing a LogLevel, or an
* object implementing the {@link @microsoft/signalr.ILogger} interface.
* See {@link
* object implementing the {@link @microsoft/signalr.ILogger} interface. See
* {@link
* https://docs.microsoft.com/aspnet/core/signalr/configuration#configure-logging|the
* documentation for client logging configuration} for more details.
* @returns The {@link @microsoft/signalr.HubConnectionBuilder} instance, for chaining.
Expand Down Expand Up @@ -187,9 +187,9 @@ exports[`Should format jsDoc default values 2`] = `
exports[`Should format regular jsDoc 1`] = `
"/**
* Function example description that was wrapped by hand so it have more then
* one line and don't end with a dot REPEATED TWO TIMES BECAUSE IT WAS EASIER
* to copy function example description that was wrapped by hand so it have
* more then one line.
* one line and don't end with a dot REPEATED TWO TIMES BECAUSE IT WAS EASIER to
* copy function example description that was wrapped by hand so it have more
* then one line.
*
* @async
* @private
Expand Down Expand Up @@ -217,9 +217,9 @@ const testFunction = (text, defaultValue, optionalNumber) => true;
exports[`Should format regular jsDoc 2`] = `
"/**
* Function example description that was wrapped by hand so it have more then
* one line and don't end with a dot REPEATED TWO TIMES BECAUSE IT WAS EASIER
* to copy function example description that was wrapped by hand so it have
* more then one line.
* one line and don't end with a dot REPEATED TWO TIMES BECAUSE IT WAS EASIER to
* copy function example description that was wrapped by hand so it have more
* then one line.
*
* @async
* @private
Expand Down
27 changes: 20 additions & 7 deletions tests/__snapshots__/paragraph.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ exports[`description contain paragraph 3`] = `
* mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur
* adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
* magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
* laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
* in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
* laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
* reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
* pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
* qui officia deserunt mollit anim id est laborum.
*/
Expand Down Expand Up @@ -211,9 +211,9 @@ exports[`description new line with dash 1`] = `
* - Stop a scroll on the left edge, then turn that into an outer view's backswipe.
* - Stop a scroll mid-bounce at the top, continue pulling to have the outer view dismiss.
* - However, without catching the scroll view mid-bounce (while it is
* motionless), if you drag far enough for the scroll view to become
* responder (and therefore drag the scroll view a bit), any backswipe
* navigation of a swipe gesture higher in the view hierarchy, should be rejected.
* motionless), if you drag far enough for the scroll view to become responder
* (and therefore drag the scroll view a bit), any backswipe navigation of a
* swipe gesture higher in the view hierarchy, should be rejected.
*/
function scrollResponderHandleTerminationRequest() {
return !this.state.observedScrollSinceBecomingResponder;
Expand Down Expand Up @@ -298,8 +298,8 @@ exports[`numbers and code in description 1`] = `
*
* A high quality interaction isn't as simple as you might think. There should
* be a slight delay before activation. Moving your finger beyond an element's
* bounds should trigger deactivation, but moving the same finger back within
* an element's bounds should trigger reactivation.
* bounds should trigger deactivation, but moving the same finger back within an
* element's bounds should trigger reactivation.
*
* 1. In order to use \`PressResponder\`, do the following:
*
Expand Down Expand Up @@ -366,3 +366,16 @@ exports[`numbers and code in description 3`] = `
*/
"
`;
exports[`printWidth 1`] = `
"/**
* A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
* A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
* A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
*
* A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
* A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
* A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
*/
"
`;
21 changes: 21 additions & 0 deletions tests/paragraph.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,27 @@ test("code in description", () => {
expect(result2).toMatchSnapshot();
});

test("printWidth", () => {
const result1 = subject(
`/**
* A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
* A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
* A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
*
* A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
* A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
* A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
*/`,
{
printWidth: 80,
},
);

expect(subject(subject(result1))).toEqual(result1);

expect(result1).toMatchSnapshot();
});

/**
* If this is a vertical ScrollView scrolls to the bottom.
* If this is a horizontal ScrollView scrolls to the right.
Expand Down

0 comments on commit 5f807ce

Please sign in to comment.