Skip to content

Commit

Permalink
comment(fix): issues of comment placement
Browse files Browse the repository at this point in the history
Signed-off-by: paul Dufour <[email protected]>
  • Loading branch information
kolowy committed Jul 3, 2023
1 parent e37e1ae commit 1ee25cc
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 64 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,12 @@ If it's a brand new type of ast node, you'll need to create a javascript file in
## Adding new functions

Check out `src/ast/ast_functions.js`. Needs documentation!

## Building the dist directory

To build the dist directory, you have to run this command :
```bash
npx webpack --config webpack.config.js
```

If you have a problem like : `Error: error:0308010C:digital envelope routines::unsupported`, the command `export NODE_OPTIONS=--openssl-legacy-provider` should fix this.
89 changes: 56 additions & 33 deletions dist/block_mirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,8 @@ BlockMirrorTextToBlocks.prototype.convertSource = function (filename, python_sou
for (var commentLocation in parse.comments) {
var lineColumn = commentLocation.split(",");
var yLocation = parseInt(lineColumn[0], 10);
this.comments[yLocation] = parse.comments[commentLocation];
var xLocation = parseInt(lineColumn[1], 10);
this.comments[yLocation] = xLocation + "|" + parse.comments[commentLocation];
}

this.highestLineSeen = 0;
Expand Down Expand Up @@ -1540,26 +1541,35 @@ BlockMirrorTextToBlocks.prototype.convertBody = function (node, parent) {
commentCount = 0;

for (var _commentLineInProgram in this.comments) {
if (_commentLineInProgram < lineNumberInProgram) {
var commentChild = this.ast_Comment(this.comments[_commentLineInProgram], _commentLineInProgram);
if (_commentLineInProgram <= lineNumberInProgram) {
var comment = this.comments[_commentLineInProgram].split("|", 2);

if (previousLineInProgram == null) {
nestChild(commentChild);
} else {
var skipped_previous_line = Math.abs(previousLineInProgram - _commentLineInProgram) > 1;
if (parseInt(comment[0], 10) / 4 == this.levelIndex - 1) {
var commentLine = comment[1];
var commentChild = this.ast_Comment(commentLine, _commentLineInProgram);
this.highestLineSeen += 1;

if (is_top_level && skipped_previous_line) {
addPeer(commentChild);
} else {
if (previousLineInProgram == null) {
nestChild(commentChild);
} else {
var skipped_previous_line = Math.abs(previousLineInProgram - _commentLineInProgram) > 1;

if (is_top_level && skipped_previous_line) {
addPeer(commentChild);
} else {
nestChild(commentChild);
}
}

previousLineInProgram = _commentLineInProgram;
this.highestLineSeen = Math.max(this.highestLineSeen, parseInt(_commentLineInProgram, 10));
distance = lineNumberInProgram - previousLineInProgram;
delete this.comments[_commentLineInProgram];
commentCount += 1;
}

previousLineInProgram = _commentLineInProgram;
this.highestLineSeen = Math.max(this.highestLineSeen, parseInt(_commentLineInProgram, 10));
distance = lineNumberInProgram - previousLineInProgram;
delete this.comments[_commentLineInProgram];
commentCount += 1;
visitedFirstLine = true;
previousWasStatement = true;
}
}

Expand Down Expand Up @@ -1596,34 +1606,47 @@ BlockMirrorTextToBlocks.prototype.convertBody = function (node, parent) {
var lastLineNumber = lineNumberInProgram + 1;

if (lastLineNumber in this.comments) {
var _commentChild = this.ast_Comment(this.comments[lastLineNumber], lastLineNumber);
var comment = this.comments[lastLineNumber].split("|", 2);

if (is_top_level && !previousWasStatement) {
addPeer(_commentChild);
} else {
nestChild(_commentChild);
}
if (parseInt(comment[0], 10) / 4 == this.levelIndex - 1) {
var lastComment = comment[1];

var _commentChild = this.ast_Comment(lastComment, lastLineNumber);

if (is_top_level && !previousWasStatement) {
addPeer(_commentChild);
} else {
nestChild(_commentChild);
}

delete this.comments[lastLineNumber];
delete this.comments[lastLineNumber];
this.highestLineSeen += 1;
}
} // Handle any extra comments that stuck around


if (is_top_level) {
for (var commentLineInProgram in this.comments) {
var _commentChild2 = this.ast_Comment(this.comments[commentLineInProgram], commentLineInProgram);
var comment = this.comments[commentLineInProgram].split("|", 2);

distance = commentLineInProgram - previousLineInProgram;
if (parseInt(comment[0], 10) / 4 == this.levelIndex - 1) {
var commentInProgram = comment[1];

if (previousLineInProgram == null) {
addPeer(_commentChild2);
} else if (distance > 1) {
addPeer(_commentChild2);
} else {
nestChild(_commentChild2);
}
var _commentChild2 = this.ast_Comment(commentInProgram, commentLineInProgram);

previousLineInProgram = commentLineInProgram;
delete this.comments[lastLineNumber];
distance = commentLineInProgram - previousLineInProgram;

if (previousLineInProgram == null) {
addPeer(_commentChild2);
} else if (distance > 1) {
addPeer(_commentChild2);
} else {
nestChild(_commentChild2);
}

previousLineInProgram = commentLineInProgram;
delete this.comments[lastLineNumber];
}
}
}

Expand Down
1 change: 1 addition & 0 deletions dist/skulpt_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4709,6 +4709,7 @@ function findInDfa(a, obj) {


Parser.prototype.addcomment = function (value, start, end, line) {
if (start[1] != line.search(/\S/)) start[1] = line.search(/\S/);
this.comments[start] = value;
}; // Add a token; return true if we're done

Expand Down
2 changes: 2 additions & 0 deletions src/skulpt/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ function findInDfa (a, obj) {

// Add a comment
Parser.prototype.addcomment = function(value, start, end, line) {
if (start[1] != line.search(/\S/))
start[1] = line.search(/\S/);
this.comments[start] = value;
};

Expand Down
89 changes: 58 additions & 31 deletions src/text_to_blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ BlockMirrorTextToBlocks.prototype.convertSource = function (filename, python_sou
for (let commentLocation in parse.comments) {
let lineColumn = commentLocation.split(",");
let yLocation = parseInt(lineColumn[0], 10);
this.comments[yLocation] = parse.comments[commentLocation];
var xLocation = parseInt(lineColumn[1], 10);
this.comments[yLocation] = xLocation + "|" + parse.comments[commentLocation];
}
this.highestLineSeen = 0;
this.levelIndex = 0;
Expand Down Expand Up @@ -215,23 +216,32 @@ BlockMirrorTextToBlocks.prototype.convertBody = function (node, parent) {
// Handle earlier comments
commentCount = 0;
for (let commentLineInProgram in this.comments) {
if (commentLineInProgram < lineNumberInProgram) {
let commentChild = this.ast_Comment(this.comments[commentLineInProgram], commentLineInProgram);
if (previousLineInProgram == null) {
nestChild(commentChild);
} else {
let skipped_previous_line = Math.abs(previousLineInProgram - commentLineInProgram) > 1;
if (is_top_level && skipped_previous_line) {
addPeer(commentChild);
} else {
if (commentLineInProgram <= lineNumberInProgram) {
var comment = this.comments[commentLineInProgram].split("|", 2);

if (parseInt(comment[0], 10) / 4 == this.levelIndex - 1) {
var commentLine = comment[1]
var commentChild = this.ast_Comment(commentLine, commentLineInProgram);
this.highestLineSeen += 1

if (previousLineInProgram == null) {
nestChild(commentChild);
} else {
let skipped_previous_line = Math.abs(previousLineInProgram - commentLineInProgram) > 1;
if (is_top_level && skipped_previous_line) {
addPeer(commentChild);
} else {
nestChild(commentChild);
}
}
previousLineInProgram = commentLineInProgram;
this.highestLineSeen = Math.max(this.highestLineSeen, parseInt(commentLineInProgram, 10));
distance = lineNumberInProgram - previousLineInProgram;
delete this.comments[commentLineInProgram];
commentCount += 1;
}
previousLineInProgram = commentLineInProgram;
this.highestLineSeen = Math.max(this.highestLineSeen, parseInt(commentLineInProgram, 10));
distance = lineNumberInProgram - previousLineInProgram;
delete this.comments[commentLineInProgram];
commentCount += 1;
visitedFirstLine = true;
previousWasStatement = true;
}
}

Expand Down Expand Up @@ -274,29 +284,46 @@ BlockMirrorTextToBlocks.prototype.convertBody = function (node, parent) {
// Handle comments that are on the very last line
var lastLineNumber = lineNumberInProgram + 1;
if (lastLineNumber in this.comments) {
let commentChild = this.ast_Comment(this.comments[lastLineNumber], lastLineNumber);
if (is_top_level && !previousWasStatement) {
addPeer(commentChild);
} else {
nestChild(commentChild);
var comment = this.comments[lastLineNumber].split("|", 2)

if (parseInt(comment[0], 10) / 4 == this.levelIndex - 1) {
var lastComment = comment[1];

let commentChild = this.ast_Comment(lastComment, lastLineNumber);

if (is_top_level && !previousWasStatement) {
addPeer(commentChild);
} else {
nestChild(commentChild);
}
delete this.comments[lastLineNumber];
this.highestLineSeen += 1
}
delete this.comments[lastLineNumber];
}

// Handle any extra comments that stuck around
if (is_top_level) {
for (var commentLineInProgram in this.comments) {
let commentChild = this.ast_Comment(this.comments[commentLineInProgram], commentLineInProgram);
distance = commentLineInProgram - previousLineInProgram;
if (previousLineInProgram == null) {
addPeer(commentChild);
} else if (distance > 1) {
addPeer(commentChild);
} else {
nestChild(commentChild);
var comment = this.comments[commentLineInProgram].split("|", 2)

if (parseInt(comment[0], 10) / 4 == this.levelIndex - 1) {
var commentInProgram = comment[1];

let commentChild = this.ast_Comment(commentInProgram, commentLineInProgram);

distance = commentLineInProgram - previousLineInProgram;

if (previousLineInProgram == null) {
addPeer(commentChild);
} else if (distance > 1) {
addPeer(commentChild);
} else {
nestChild(commentChild);
}

previousLineInProgram = commentLineInProgram;
delete this.comments[lastLineNumber];
}
previousLineInProgram = commentLineInProgram;
delete this.comments[lastLineNumber];
}
}

Expand Down

0 comments on commit 1ee25cc

Please sign in to comment.