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

feat: support hover tooltip for scss #367

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/services/cssHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,23 @@ export class CSSHover {
* Build up the hover by appending inner node's information
*/
let hover: Hover | null = null;
let flagOpts:{text:string;isMedia:boolean};

for (let i = 0; i < nodepath.length; i++) {
const node = nodepath[i];

if (node instanceof nodes.Media){
Copy link
Contributor

Choose a reason for hiding this comment

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

Please run the formatter (default Typescript formatter)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this ok now, I runned default prettier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I rise an issue for format script. It might helps other engineers to follow common format configuration.

const regex = /@media[^\{]+/g;
const matches = node.getText().match(regex);
flagOpts = {
isMedia:true,
text:matches?.[0].toString()!
Copy link
Contributor

Choose a reason for hiding this comment

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

toString()! is not necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I removed it.

};
}

if (node instanceof nodes.Selector) {
hover = {
contents: this.selectorPrinting.selectorToMarkedString(<nodes.Selector>node),
contents: this.selectorPrinting.selectorToMarkedString(<nodes.Selector>node, flagOpts!),
range: getRange(node)
};
break;
Expand Down Expand Up @@ -149,7 +159,7 @@ export class CSSHover {
return contents.value;
}
}

return contents;
}

Expand Down
15 changes: 10 additions & 5 deletions src/services/selectorPrinting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,21 @@ class MarkedStringPrinter {
// empty
}

public print(element: Element): MarkedString[] {
public print(element: Element, flagOpts?:{isMedia: boolean, text: string}): MarkedString[] {
this.result = [];
if (element instanceof RootElement) {
if (element.children) {
this.doPrint(element.children, 0);
}
} else {
this.doPrint([element], 0);
}
let value ;
if(flagOpts){
value = `${flagOpts.text}\n … ` + this.result.join('\n');
}else{
value = this.result.join('\n');
}

const value = this.result.join('\n');
return [{ language: 'html', value }];
}

Expand Down Expand Up @@ -325,14 +329,15 @@ function unescape(content: string) {


export class SelectorPrinting {

constructor(private cssDataManager: CSSDataManager) {

}

public selectorToMarkedString(node: nodes.Selector): MarkedString[] {
public selectorToMarkedString(node: nodes.Selector, flagOpts?:{isMedia: boolean, text: string}): MarkedString[] {
const root = selectorToElement(node);
if (root) {
const markedStrings = new MarkedStringPrinter('"').print(root);
const markedStrings = new MarkedStringPrinter('"').print(root, flagOpts);
markedStrings.push(this.selectorToSpecificityMarkedString(node));
return markedStrings;
} else {
Expand Down
10 changes: 10 additions & 0 deletions src/test/css/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ suite('SCSS Hover', () => {
},
'scss'
);
assertHover(
'.foo{ .bar{ @media only screen{ .|bar{ } } } }',
{
contents: [
{ language: 'html', value: '@media only screen\n … <element class=\"foo\">\n …\n <element class=\"bar\">\n …\n <element class=\"bar\">' },
'[Selector Specificity](https://developer.mozilla.org/docs/Web/CSS/Specificity): (0, 1, 0)'
]
},
'scss'
);
});

test('@at-root', () => {
Expand Down