Skip to content

Commit

Permalink
Added overflowWrap
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremiah-shaulov committed Oct 1, 2022
1 parent 93369e7 commit dc72d2a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 13 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Finds and replaces common indent, and hard-wraps text. Can work on text that con
# Example

```ts
import {indentAndWrap} from 'https://deno.land/x/indent_and_wrap@v0.0.6/mod.ts';
import {indentAndWrap} from 'https://deno.land/x/indent_and_wrap@v0.0.7/mod.ts';

console.log
( indentAndWrap
Expand Down Expand Up @@ -41,6 +41,7 @@ type IndentAndWrapOptions =
indent?: string;
ignoreFirstIndent?: boolean;
wrapWidth?: number;
overflowWrap?: boolean;
tabWidth?: number;
mode?: 'plain' | 'term';
};
Expand All @@ -54,6 +55,7 @@ If `options.ignoreFirstIndent` is set, will look for common indent starting at s
If you already know the common indent (e.g. you called `findCommonIndent()`), you can provide it as `knownCommonIndent` to save some calculation time.
If `knownCommonIndent` doesn't match the result of `findCommonIndent()`, the behavior is undefined.
- If `options.wrapWidth` is set, it inserts `options.endl`, so there're no lines longer than `options.wrapWidth` columns. Columns are calculated with respect to `options.tabWidth` (default 4).
If `options.overflowWrap` is set, can break long words, that are wider than `options.overflowWrap`.

## getTextRect()

Expand All @@ -64,6 +66,7 @@ type GetTextRectOptions =
{ indent?: string;
ignoreFirstIndent?: boolean;
wrapWidth?: number;
overflowWrap?: boolean;
tabWidth?: number;
mode?: 'plain' | 'term';
};
Expand Down
30 changes: 18 additions & 12 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ export type IndentAndWrapOptions =
indent?: string;
ignoreFirstIndent?: boolean;
wrapWidth?: number;
overflowWrap?: boolean;
tabWidth?: number;
mode?: 'plain' | 'term';
};

export type GetTextRectOptions =
{ indent?: string;
ignoreFirstIndent?: boolean;
wrapWidth?: number;
overflowWrap?: boolean;
tabWidth?: number;
mode?: 'plain' | 'term';
};
Expand All @@ -27,14 +37,6 @@ export type CalcLinesOptions =
mode?: 'plain' | 'term';
};

export type GetTextRectOptions =
{ indent?: string;
ignoreFirstIndent?: boolean;
wrapWidth?: number;
tabWidth?: number;
mode?: 'plain' | 'term';
};

/** This function does:
- Replaces new line characters (`\n`, `\r\n` or `\r`) to `options.endl`, or if it's not set to `\n`.
- If `options.indent` is set, it determines common indent characters across all lines, and replaces them with `options.indent` string.
Expand All @@ -60,6 +62,7 @@ function doIndentAndWrap(isRect: boolean, text: string, options?: IndentAndWrapO
{ let indent = options?.indent;
const ignoreFirstIndent = options?.ignoreFirstIndent || false;
const wrapWidth = options?.wrapWidth || Number.MAX_SAFE_INTEGER;
const overflowWrap = options?.overflowWrap || false;
const tabWidth = Math.max(1, Math.min(16, options?.tabWidth || 4));
const endl = options?.endl || '\n';
const isTerm = options?.mode == 'term';
Expand Down Expand Up @@ -96,7 +99,7 @@ function doIndentAndWrap(isRect: boolean, text: string, options?: IndentAndWrapO
let nLines = lastChar==C_CR || lastChar==C_LF ? 1 : 0;
let nColumns = 0;
while (i < length)
{ const {n, endlLen, isBlankLine} = scanLine(text, i, wantWidth-minusIndent, tabWidth, commonIndentCol, indentCol, isTerm);
{ const {n, endlLen, isBlankLine} = scanLine(text, i, wantWidth-minusIndent, overflowWrap, tabWidth, commonIndentCol, indentCol, isTerm);
if (!isBlankLine)
{ if (!isRect)
{ res += indent + text.slice(i+skip, n-endlLen);
Expand Down Expand Up @@ -290,7 +293,7 @@ function precedingSpaceLen(text: string, i: number, isTerm: boolean)

/** Returns position where next line begins, and length of line-break characters at the end of the skipped line.
**/
function scanLine(text: string, i: number, wrapWidth: number, tabWidth: number, removeIndentCol: number, addIndentCol: number, isTerm: boolean)
function scanLine(text: string, i: number, wrapWidth: number, overflowWrap: boolean, tabWidth: number, removeIndentCol: number, addIndentCol: number, isTerm: boolean)
{ const {length} = text;
let col = 0;
let wordEnd = 0;
Expand Down Expand Up @@ -333,7 +336,7 @@ function scanLine(text: string, i: number, wrapWidth: number, tabWidth: number,
break;


case C_BEL: // deno-lint-ignore no-fallthrough
case C_BEL:
case C_ESC:
if (isTerm)
{ if (c == C_BEL)
Expand All @@ -345,11 +348,14 @@ function scanLine(text: string, i: number, wrapWidth: number, tabWidth: number,
continue;
}
}
// fallthrough

default:
col++;
if (col > wrapWidth)
{ return {n: wordEnd || i, endlLen: 0, isBlankLine: wordEnd==0 && prevIsSpace};
{ if (wordEnd || overflowWrap)
{ return {n: wordEnd || i, endlLen: 0, isBlankLine: wordEnd==0 && prevIsSpace};
}
}
prevIsSpace = false;
}
Expand Down
64 changes: 64 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,73 @@ Deno.test
( '\n Line 1\n \n Line 2222\n',
{indent: '****', wrapWidth: 7}
),
'\n****Line\n****1\n\n****Line\n****2222\n'
);

assertEquals
( indentAndWrap
( '\n Line 1\n \n Line 2222\n',
{indent: '****', wrapWidth: 7, overflowWrap: true}
),
'\n****Lin\n****e 1\n\n****Lin\n****e\n****222\n****2\n'
);

assertEquals
( indentAndWrap
( 'abcdefghi',
{wrapWidth: 3, overflowWrap: true}
),
'abc\ndef\nghi'
);

assertEquals
( indentAndWrap
( 'abcdefghi\n',
{wrapWidth: 3, overflowWrap: true}
),
'abc\ndef\nghi\n'
);

assertEquals
( indentAndWrap
( 'abcdefghi\nab cdef',
{wrapWidth: 3, overflowWrap: true}
),
'abc\ndef\nghi\nab\ncde\nf'
);

assertEquals
( indentAndWrap
( 'abcdefghi\n',
{indent: ' ', wrapWidth: 3, overflowWrap: true}
),
' a\n b\n c\n d\n e\n f\n g\n h\n i\n'
);

assertEquals
( indentAndWrap
( 'abcdefghi\n',
{indent: ' ', wrapWidth: 3, overflowWrap: true}
),
' a\n b\n c\n d\n e\n f\n g\n h\n i\n'
);

assertEquals
( indentAndWrap
( 'ab\tcde',
{wrapWidth: 3, overflowWrap: true}
),
'ab\ncde'
);

assertEquals
( indentAndWrap
( 'ab\tcde',
{indent: ' ', wrapWidth: 3, overflowWrap: true}
),
' a\n b\n c\n d\n e'
);

assertEquals
( indentAndWrap
( '\nLine\t1\nLin\t2\n',
Expand Down

0 comments on commit dc72d2a

Please sign in to comment.