csscomb / sublime-csscomb

Sublime plugin for CSScomb—CSS coding style formatter
398 stars 61 forks source link

Sublime Text: Does not work when you select a block of text #96

Closed smlombardi closed 5 years ago

smlombardi commented 10 years ago
  1. selected a block of code
  2. apply hotkey. extra line is added, but no sorting. see attached animated GIF

csscomb

Mac 10.9.1 ST3 3059 Stable. Nothing logged in console.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/1519034-sublime-text-does-not-work-when-you-select-a-block-of-text?utm_campaign=plugin&utm_content=tracker%2F214563&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F214563&utm_medium=issues&utm_source=github).
tonyganch commented 10 years ago

What config do you use?

smlombardi commented 10 years ago

not sure. just the default, I suppose. I never set one up. I used to use the php version, then I installed the js version last week.

On Fri, Feb 14, 2014 at 4:33 PM, Tony Ganch notifications@github.comwrote:

What config do you use?

Reply to this email directly or view it on GitHubhttps://github.com/csscomb/csscomb.js/issues/177#issuecomment-35126900 .

tonyganch commented 10 years ago

I managed to reproduce this, thank you.

The problem is with parser, which treats selected text as valid css. Since a bunch of declarations is not a valid rule, CSScomb fails to sort anything. If you select the whole rule (including .utility-nav {...}), it should work fine.

I can't see a way to tell the parser which part of css you want to comb: a rule or a list of properties. It means I can't think of a way to fix this.

kizu commented 10 years ago

I can see three ways of fixing this:

  1. Editor way — we can get the whole scope of the selected region (including the selector), then remove all the non-selected things, pass it to the CSSComb.js, comb it, then remove the wrapper and insert the combed code into the selection. This is rather easy to do on the ST side.
  2. Do the similar thing in the parser: pass the line and column numbers of the selected region alongside with the whole file, then parser would parse the whole thing till the block with the selected region, parse it and stop at it, then do the stuff I described in the previous variant. While it would be the most bullet-proof way, it would be also the least efficient (we would need to parse the whole file up to the selected region).
  3. Combine the two previous variants: first, detect the scope in the editor — is this a declaration, rule block, root level, then run CSSComb.js with the context param, that would tell the parser in which context we are. This would actually would be a useful addition to the parser: we could then use it not only for the whole stylesheets, but also for any tools that are working with any individual items of the stylesheets, so if we'd like, we could parse a single declaration, or a part of the selector, or an rgba function, or whatever.

I'll vote for the third variant as it would be an overall enhancement to the parser, and I personally could then update the ST plugin so it would detect and pass the context of the selection to the parser.

tonyganch commented 10 years ago

@kizu, well, be my guest. Plugin's source code: https://github.com/csscomb/sublime-csscomb And Gonzales already accepts rule parameter: https://github.com/tonyganch/gonzales-pe/blob/master/doc/Gonzales-Usage.md#css---ast

tonyganch commented 10 years ago

You know, I've implemented this "comb a selected part" feature in Sublime only because it'd been there in original (php) plugin. Now, after giving it some thought, I can't see a reason for the feature to be there at all. Can anyone give me a use case, please?

kizu commented 10 years ago

Case 1. I can suppose that in some cases (in preprocessors' code) you wouldn't want to comb everything, cause something could possibly go wrong after the sorting. For example, if this code:

@mixin foo {
  width: 10px
}

.bar {
  @include foo;
  width: 20px;
}

would be combed to

@mixin foo {
    width: 10px;
}

.bar
{
    width: 20px;

  @include foo;
} 

then the resulting CSS would be different from the initial one. So, you wouldn't want to call CSSComb for the whole file, but would want to comb things you know wouldn't break.

Case 2. If you have a really big CSS file and don't want to run CSSComb through the whole file all the time, you could run it only for the new code you wrote.

Case 3. A developer of a foreign plugin could want to use CSSComb to comb the output of his/her plugin. For example, a plugin inserting snippets could want to insert them with the current detected style, but he wouldn't want the whole file to be combed. So this plugin would at first detect the style using CSSComb and the whole file, then insert new styles and then comb only them. Of course, the developer could do this to not-yet-inserted code, but this still would need the #179 to be implemented in order to comb only declarations etc.

Case 4. You could want to add new styles to a stylesheet that have a lot of bad uncombed code without any given code style. In this case there is no code style to use, so you could use any code style for your new code. In this case you could comb it by selecting it, but you wouldn't want to comb the whole stylesheet.

smlombardi commented 10 years ago

I used case 1 all the time

On Monday, February 17, 2014, Roman Komarov notifications@github.com wrote:

Case 1. I can suppose that in some cases (in preprocessors' code) you wouldn't want to comb everything, cause something could possibly go wrong after the sorting. For example, if this code:

@mixin foo { width: 10px} .bar { @include foo; width: 20px;}

would be combed to

@mixin foo { width: 10px;} .bar{ width: 20px;

@include foo;}

then the resulting CSS would be different from the initial one. So, you wouldn't want to call CSSComb for the whole file, but would want to comb things you know wouldn't break.

Case 2. If you have a really big CSS file and don't want to run CSSComb through the whole file all the time, you could run it only for the new code you wrote.

Case 3. A developer of a foreign plugin could want to use CSSComb to comb the output of his/her plugin. For example, a plugin inserting snippets could want to insert them with the current detected style, but he wouldn't want the whole file to be combed. So this plugin would at first detect the style using CSSComb and the whole file, then insert new styles and then comb only them. Of course, the developer could do this to not-yet-inserted code, but this still would need the #179https://github.com/csscomb/csscomb.js/pull/179to be implemented in order to comb only declarations etc.

Case 4. You could want to add new styles to a stylesheet that have a lot of bad uncombed code without any given code style. In this case there is no code style to use, so you could use any code style for your new code. In this case you could comb it by selecting it, but you wouldn't want to comb the whole stylesheet.

Reply to this email directly or view it on GitHubhttps://github.com/csscomb/csscomb.js/issues/177#issuecomment-35235947 .

tonyganch commented 10 years ago

cause something could possibly go wrong after the sorting

If there are bugs they should be fixed but not avoided. The example above will be ok in version 3.0, for example.

If you have a really big CSS file

Firstly, you don't run CSScomb every 5 seconds. Secondly, if the tool works so slow it hinders your work, this is a performance problem and should be fixed.

A developer of a foreign plugin could want to use CSSComb to comb the output of his/her plugin

I have nothing against adding context parameter to processString() method. But it has nothing to do with Sublime plugin.

but you wouldn't want to comb the whole stylesheet

Why?

jdalton commented 5 years ago

Closing as stale. If anyone is up for creating a PR head over to sublime-csscomb or open one at csscomb if needed.