Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any possibility of multi-language awareness? #2

Open
etjones opened this issue Apr 5, 2019 · 17 comments
Open

Any possibility of multi-language awareness? #2

etjones opened this issue Apr 5, 2019 · 17 comments

Comments

@etjones
Copy link

etjones commented Apr 5, 2019

Thanks for this project; it replaces some old functionality I missed from TextMate. However, I think I need to change my settings manually depending on the language I'm using. For example,
in C* or Javascript, I want:

// ====================
// = C-STYLE COMMENTS =
// ====================

and in Python or Shell, I want:

# =========================
# = PYTHON-STYLE COMMENTS =
# =========================

But the only desired difference is starting lines with // or #. Right now I've been changing three settings, ( commentStartToken, leftEdgeToken and bottomLeftToken) every time I switch languages. Would it be possible to change this automatically based on the current language grammar?

@slysherz
Copy link
Owner

slysherz commented Apr 5, 2019

Hello, I'm glad you found it useful. About this issue, there is currently an open issue for VS Code that would solve the problem (microsoft/vscode#26707), so I don't think it would be a good idea to try re-implementing the same thing.

That being said, I do currently have plans to add support for setting up multiple box styles at the same time and being able to select one from a dropdown menu when using the extension. Possible example (doesn't work yet):

"commentBox.styles": {
    "python": {
        "commentStartToken": "#"
    },
    "c": {
        "commentStartToken": "//"
    }
}

Even though this has a slightly different use case, I believe it will ease most of the pain until the VS Code feature lands. Please let me know what you think, thanks!

@etjones
Copy link
Author

etjones commented Apr 5, 2019

Thanks, that sounds like there's definitely a way forward. TextMate (& Sublime, following them) had a bigger range of dynamic variables available (e.g. TM_COMMENT_START or TM_SCOPE) that allowed easier per-language customization. Until or unless VS Code gets a richer set of context variables, I think that defining multiple command settings would be great. That way we could define one key binding for one comment style and another binding for a different one. Cheers!

@slysherz
Copy link
Owner

slysherz commented Jun 3, 2019

Comment Box version 2.0.0 is out and it includes support for multiple styles. Enjoy : )

@etjones
Copy link
Author

etjones commented Jun 3, 2019

Nice. Can't wait to try it out. Thanks for this!

@etjones etjones closed this as completed Jun 3, 2019
@Enteleform
Copy link

@etjones

It seems that microsoft/vscode#26707 has been closed, and there are also other plugins like Comment Divider that have language awareness.

Can you reopen this issue?

@Enteleform
Copy link

Enteleform commented Jul 24, 2020

Also, a nice setting to have for this would be something like:

"commentStart": "block",   // in JS, equivalent to `/*`
"commentEnd":   "block",   // in JS, equivalent to `*/`

"commentStart": "inline",  // in JS, equivalent to `//`
"commentEnd":   "inline",  // in JS, equivalent to `//`

For example, I would use the inline setting for both commentStart and commentEnd to achieve the styles that I'm currently using:

Code_2020-07-24_15-26-35

@slysherz slysherz reopened this Jul 25, 2020
@slysherz
Copy link
Owner

I've looked into this and I have some news.

There's currently an issue in the backlog to allow extensions to have access to the Language Configuration, which includes among other things, which characters to use to start and end both line and block comments.

I think it's better to wait for this than to implement and maintain a table with the characters used by each language.

That said, there's a feature I've been thinking about that doesn't quite solve the problem, but it should alleviate it: it should be possible to say: "this style is like that other one, but use this character here instead". Something like this:

{
    "SingleLine--ASCII-Light": {
        // ...
    },
    "SingleLine--ASCII-Heavy": {
        "basedOn": "SingleLine--ASCII-Light",
        "commentStartToken": "//###  ",
        "commentEndToken": "  ###//"
    }
}

With this feature, you could have a basic style with start and end tokens, and build other styles on top. You still need to change it every time you change languages, but only in one place:

{
    "baseStyle": {
        "commentStartToken": "/**",
        "commentEndToken": "**/"
    },
    "styleA": {
        "basedOn": "baseStyle",
        // ...
    }
    "styleB": {
        "basedOn": "baseStyle",
        // ...
    }
}

Please let me know what you think, thanks!

@Enteleform
Copy link

Enteleform commented Aug 22, 2020

@slysherz

 
The basedOn inheritance feature is an excellent idea!

The alleviation of the language-awareness issue would be minor, as my primary workflow involves having specific Comment Box styles bound to hotkeys. I guess for the meantime I could just create duplicate sets of my styles & utilize them via commentBox.transformUsingStyle when I'm working in languages that don't use // for comments. basedOn would help here, but is not a necessity, as it's a temporary workaround with unnecessary duplication anyway.

 
However, basedOn would overall be a great addition that would greatly improve the configuration of Comment Box.

I just optimized my configuration file based on the following considerations:

  • basedOn is implemented and accepts arrays. Styles are merged with rightmost entries taking precedence, maintaining consistency with JavaScript's object spread conventions. This allows users to build modular "configuration" (see: _BaseStyle, _SingleLine, etc.) and "character" (see: _ASCII-Light, _Unicode-Heavy, etc.) templates that can be mixed & matched into actual styles (see: SingleLine--ASCII-Light, MultiLine--Unicode-Light, etc.).
  • commentBox.transformUsingStyle ignores styles prefixed with an _, or there is some boolean setting like [excludeFromList, private, hidden, display, etc.] that allows styles to be excluded from the command palette.
  • Language awareness has been implemented, with the following transformations for logical consistency:
      - commentStartToken > topLeftToken     // to match existing character-set naming convention
      - commentEndToken   > bottomRightToken // to match existing character-set naming convention
      
      - commentStart: ("inline" | "block" | "none")
      - commentEnd:   ("inline" | "block" | "none")

These features allowed me to reduce my configuration lines from 312 to 64!
(since I can now build out character-sets horizontally and see them all at once, due to omission of "configuration" properties.)

@Gist

CommentBox


 

Edit

Upon further reflection, it might be a good idea to update comment* & *Token into objects to improve readability/conciseness and eliminate redundancy.

Before:
"_ASCII-Light": {"commentStart":"inline", "commentEnd":"inline", "topLeftToken":"--", "topEdgeToken":"-", "topRightToken":"--", "bottomLeftToken":"--", "bottomEdgeToken":"-", "bottomRightToken":"--", "leftEdgeToken":"  ", "rightEdgeToken":"  "},

After:
"_ASCII-Light": {"comment":{"start":"inline", "end":"inline"}, "tokens":{"topLeft":"--", "top":"-", "topRight":"--", "bottomLeft":"--", "bottom":"-", "bottomRight":"--", "left":"  ", "right":"  "}},

The proposed structure reduces the line length by 47 characters! This allows the entire line to be visible on my 27" 16:9 monitor, whereas before it was only possible to see the entire line on my 34" 21:9 ultrawide monitor.

 
Also, (in the context of this proposal) since [commentStartToken, commentEndToken] are no longer relevant, and have been replaced by tokens:{topLeft, bottomRight}, this would make creating single line styles unintuitive. I think it might make more sense for tokens:{left, right} to be applied for single line text replacements.

@Enteleform
Copy link

there is currently an open issue for VS Code that would solve the problem (microsoft/vscode#26707)

Seems like this should now be possible, any chance the new functionality can be implemented now?

@slysherz
Copy link
Owner

Yes, I'll look into this now : )

slysherz pushed a commit that referenced this issue Feb 13, 2023
@slysherz
Copy link
Owner

Alright, I have this almost ready. Just need to test it a bit more, then release. In case someone wants to help, here's a preview:

https://github.com/slysherz/vscode-comment-box/raw/master/vsix_releases/comment-box-2.3.0.vsix

@etjones This now works with:

{
    "commentBox.styles": {
        "defaultStyle": {
            "topEdgeToken": "=",
            "rightEdgeToken": " =",
            "topRightToken": "==",
            "commentEndToken": "==",
            "bottomEdgeToken": "="
        },
    },
    "[javascript][cpp][c]": {
        "commentBox.styles": {
            "defaultStyle": {
                "commentStartToken": "// ",
                "leftEdgeToken": "// = ",
                "bottomLeftToken": "// "
            }
        },
    },
    "[python]": {
        "commentBox.styles": {
            "defaultStyle": {
                "commentStartToken": "# ",
                "leftEdgeToken": "# = ",
                "bottomLeftToken": "# "
            }
        }
    },
}

I tried to add default styles for multiple languages, but it doesn't work well, the language default overrides user (non language) configuration.

@Enteleform
Copy link

In case someone wants to help, here's a preview:

Got back from a trip last night and just now saw this, thanks for working on it!

Seems to be working well:
image

@iandotmartin
Copy link

looks like the latest version available through vscode is still 2.2.1 - would love to have this functionality without manually installing!

@slysherz
Copy link
Owner

Hey @iandotmartin , thanks for the reminder. The new version is up, enjoy : )

@iandotmartin
Copy link

works great! thanks very much 🥳

@slysherz
Copy link
Owner

One issue I ran into, but didn't manage to fix in time: the extension should set default values such that comment boxes appear correctly for each language, for example this should be the default for python:

#######
# BOX # 
#######

But I found an issue, setting default values for a specific language has priority over general settings by the user. I didn't want to override anyone's settings, so I didn't add defaults. If anyone has thoughts / ideas about this, please let me know.

@iandotmartin
Copy link

👍 for a valid default style per language

fwiw in my usage i found that i needed to set up a style for each new language anyways, as the global default was often invalid. it's also pretty easy to override for a given language and/or map a keybind to the one global comment box style you want if that's the desired behavior.

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

No branches or pull requests

4 participants