Skip to content

Commit

Permalink
Fix the case when an emphasis start and end token are not of the same…
Browse files Browse the repository at this point in the history
… length.
  • Loading branch information
drgrice1 committed Nov 23, 2024
1 parent 454d370 commit ba972ad
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openwebwork/codemirror-lang-pg",
"version": "0.0.1-beta.16",
"version": "0.0.1-beta.18",
"description": "PG language support for CodeMirror",
"author": "The WeBWorK Project",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/pgml-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,14 @@ export class PGMLParse {
const type = BlockDefs[token.substring(0, 1)]?.type;
let block = this.block;
if (block && block.type === type) {
block.to += block.token?.length ?? 1;
block.to += token.length;
this.Terminate();
return;
}
while (block?.type !== 'root') {
if (block && block.prev?.type === type) {
this.End(`end of ${type ?? ''}`, block);
block.to += block.token?.length ?? 1;
block.to += token.length;
this.Terminate();
return;
}
Expand Down
9 changes: 8 additions & 1 deletion src/pgml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,14 @@ const pgmlFormat = (block: Item, offset: number): Element<Type>[] => {
const lastChild = block.stack?.at(-1);
if (lastChild instanceof Item && block.to === lastChild.to)
children.push(elt(Type.PGMLError, block.to + offset, block.to + offset));
else children.push(elt(Type.EmphasisMark, block.to - (block.token?.length ?? 1) + offset, block.to + offset));
else
children.push(
elt(
Type.EmphasisMark,
(lastChild instanceof Item ? lastChild.to : block.to - (block.token?.length ?? 1)) + offset,
block.to + offset
)
);
return [
elt(
block.type === 'bold' ? Type.StrongEmphasis : Type.Emphasis,
Expand Down
30 changes: 15 additions & 15 deletions test/pgml-formatting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ END_PGML
{
"name": "Paragraph",
"from": 61,
"to": 124,
"to": 125,
"children": [
{
"name": "StrongEmphasis",
"from": 108,
"to": 123,
"to": 124,
"children": [
{ "name": "EmphasisMark", "from": 108, "to": 111 },
{ "name": "EmphasisMark", "from": 120, "to": 123 }
{ "name": "EmphasisMark", "from": 120, "to": 124 }
]
}
]
Expand Down Expand Up @@ -138,7 +138,7 @@ stack: [
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
from: '50'
indent: '0'
to: '113'
to: '114'
type: 'indent'
stack: [
[ # 0
Expand All @@ -150,7 +150,7 @@ stack: [
]
[ # 1
from: '97'
to: '112'
to: '113'
token: '***'
type: 'bold'
stack: [
Expand All @@ -165,8 +165,8 @@ stack: [
]
[ # 2
combine: { text: 'type' }
from: '112'
to: '113'
from: '113'
to: '114'
type: 'text'
stack: ['\n']
]
Expand Down Expand Up @@ -198,7 +198,7 @@ END_PGML
{
"name": "PGMLContent",
"from": 10,
"to": 91,
"to": 90,
"children": [
{
"name": "Paragraph",
Expand All @@ -219,15 +219,15 @@ END_PGML
{
"name": "Paragraph",
"from": 43,
"to": 91,
"to": 90,
"children": [
{
"name": "Emphasis",
"from": 71,
"to": 90,
"to": 89,
"children": [
{ "name": "EmphasisMark", "from": 71, "to": 75 },
{ "name": "EmphasisMark", "from": 86, "to": 90 }
{ "name": "EmphasisMark", "from": 86, "to": 89 }
]
}
]
Expand Down Expand Up @@ -289,7 +289,7 @@ stack: [
combine: { indent: 'indent', list: { indent: '1' }, par: 'true' }
from: '32'
indent: '0'
to: '80'
to: '79'
type: 'indent'
stack: [
[ # 0
Expand All @@ -301,7 +301,7 @@ stack: [
]
[ # 1
from: '60'
to: '79'
to: '78'
token: '____'
type: 'italic'
stack: [
Expand All @@ -316,8 +316,8 @@ stack: [
]
[ # 2
combine: { text: 'type' }
from: '79'
to: '80'
from: '78'
to: '79'
type: 'text'
stack: ['\n']
]
Expand Down

0 comments on commit ba972ad

Please sign in to comment.