Skip to content
This repository was archived by the owner on Oct 1, 2019. It is now read-only.

Add support for comments #9

Merged
merged 1 commit into from
Dec 26, 2017
Merged

Add support for comments #9

merged 1 commit into from
Dec 26, 2017

Conversation

vjeux
Copy link
Contributor

@vjeux vjeux commented Dec 25, 2017

This wires up the comment logic from prettier. This needs some modification inside of prettier itself, so it won't work as is.

        (node.kind && node.kind !== "Comment") ||
        node.ast_type && node.ast_type !== "comment") // <-- added
    ) {
  switch (comment.type || comment.kind || comment.ast_type) {
    case "comment":
      return comment.value;
    case "Comment":

If we tweak those two small things in prettier itself, then it's able to print comments using the heuristic. @azz, do you think you'd be able to extract those out to be parser-specific?

This wires up the comment logic from prettier. This needs some modification inside of prettier itself, so it won't work as is.

- locStart/End is ast-specific, we need a way to set it up for each parser. In this case I reused on that already existed: https://github.com/prettier/prettier/blob/master/src/util.js#L260-L262

- getSortedChildNodes as well, need a way to traverse the ast: https://github.com/prettier/prettier/blob/master/src/comments.js#L38

```js
        (node.kind && node.kind !== "Comment") ||
        node.ast_type && node.ast_type !== "comment") // <-- added
    ) {
```

- printComments as well: https://github.com/prettier/prettier/blob/master/src/comments.js#L928-L929

```js
  switch (comment.type || comment.kind || comment.ast_type) {
    case "comment":
      return comment.value;
    case "Comment":
```

If we tweak those two small things in prettier itself, then it's able to print comments using the heuristic. @azz, do you think you'd be able to extract those out to be parser-specific?
@vjeux vjeux merged commit b61ddd6 into prettier:master Dec 26, 2017
# a
)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if a: # a
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't quite correct. PEP 8 calls for two spaces before inline comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an odd requirement :)

Copy link
Contributor

Choose a reason for hiding this comment

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

See https://www.python.org/dev/peps/pep-0008/#inline-comments

Inline comments should be separated by at least two spaces from the statement.

Yes, it's sort of weird, but it's more or less universal in Python-land.

'value': token.string,
'start': token.startpos,
'end': token.endpos,
} for token in atok.tokens if token.type == 57]
Copy link
Contributor

Choose a reason for hiding this comment

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

should be tokenize.COMMENT, which is 53 on py2

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants