Skip to content

Commit 6010236

Browse files
committed
Add support for navigating to element tagname
1 parent c87212b commit 6010236

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

packages/language-server/src/plugins/typescript/CSSClassDefinitionLocator.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export class CSSClassDefinitionLocator {
1818
return this.getStandardFormatClassName();
1919
}
2020

21+
if (this.isTargetHTMLElement()) {
22+
return this.getHTMlElementName();
23+
}
24+
2125
if (this.isDirectiveFormat() && this.initialNodeAt.name) {
2226
return this.getDefinitionRangeWithinStyleSection(`.${this.initialNodeAt.name}`);
2327
}
@@ -41,6 +45,18 @@ export class CSSClassDefinitionLocator {
4145
return false;
4246
}
4347

48+
/**
49+
* HTML Element target:
50+
* <main>
51+
*/
52+
private isTargetHTMLElement() {
53+
if (this.initialNodeAt?.type == 'Element') {
54+
return true;
55+
}
56+
57+
return false;
58+
}
59+
4460
/**
4561
* Conditional Expression format:
4662
* class="{current === 'baz' ? 'selected' : ''
@@ -104,8 +120,25 @@ export class CSSClassDefinitionLocator {
104120
return this.getDefinitionRangeWithinStyleSection(`.${cssClassName}`);
105121
}
106122

123+
private getHTMlElementName() {
124+
const result = this.getDefinitionRangeWithinStyleSection(`${this.initialNodeAt.name}`);
125+
if (result) {
126+
//Shift start/end to get the highlight right
127+
const originalStart = result.start.character;
128+
result.start.character -= 1;
129+
if (this.initialNodeAt.name) {
130+
result.end.character = originalStart + this.initialNodeAt.name.length;
131+
}
132+
133+
return result;
134+
}
135+
}
136+
107137
private getDefinitionRangeWithinStyleSection(targetClass: string) {
108-
let indexOccurence = this.document.content.indexOf(targetClass, 0);
138+
let indexOccurence = this.document.content.indexOf(
139+
targetClass,
140+
this.document.styleInfo?.start
141+
);
109142

110143
while (indexOccurence >= 0) {
111144
if (this.isOffsetWithinStyleSection(indexOccurence)) {
@@ -124,6 +157,8 @@ export class CSSClassDefinitionLocator {
124157
}
125158

126159
return targetRange;
160+
} else {
161+
break;
127162
}
128163
}
129164
}

0 commit comments

Comments
 (0)