Skip to content

Commit 4fafecc

Browse files
committed
fix: element getting highlighted on empty line in css file
1 parent 9f87a18 commit 4fafecc

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/language/CSSUtils.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ define(function (require, exports, module) {
15901590
function findSelectorAtDocumentPos(editor, pos) {
15911591
var cm = editor._codeMirror;
15921592
var ctx = TokenUtils.getInitialContext(cm, $.extend({}, pos));
1593-
var selector = "", foundChars = false;
1593+
var selector = "", foundChars = false, encounteredSemicolon = false;
15941594
var isPreprocessorDoc = isCSSPreprocessorFile(editor.document.file.fullPath);
15951595
var selectorArray = [];
15961596

@@ -1687,6 +1687,12 @@ define(function (require, exports, module) {
16871687
break;
16881688
}
16891689
} else {
1690+
// Track semicolons encountered before any selector content.
1691+
// A ; at the top level (e.g. after @import) means the cursor
1692+
// is after a complete statement, not inside a selector.
1693+
if (!isPreprocessorDoc && ctx.token.string === ";" && !foundChars) {
1694+
encounteredSemicolon = true;
1695+
}
16901696
if (!isPreprocessorDoc && _hasNonWhitespace(ctx.token.string)) {
16911697
foundChars = true;
16921698
}
@@ -1699,6 +1705,22 @@ define(function (require, exports, module) {
16991705

17001706
selector = _stripAtRules(selector);
17011707

1708+
// If no selector found and cursor is on a line that contains only },
1709+
// find the rule that this } closes. The backward scan may have missed
1710+
// } when the cursor is at ch:0 (getTokenAt returns an empty token),
1711+
// or it may have hit } and broken without parsing.
1712+
if (!selector && !isPreprocessorDoc) {
1713+
var closingLineText = cm.getLine(pos.line).trim();
1714+
if (closingLineText === "}") {
1715+
var braceCtx = TokenUtils.getInitialContext(cm,
1716+
{line: pos.line, ch: cm.getLine(pos.line).indexOf("}") + 1});
1717+
_skipToOpeningBracket(braceCtx, "}");
1718+
if (braceCtx.token.string === "{") {
1719+
selector = _stripAtRules(_parseSelector(braceCtx));
1720+
}
1721+
}
1722+
}
1723+
17021724
// Reset the context to original scan position
17031725
ctx = TokenUtils.getInitialContext(cm, $.extend({}, pos));
17041726

@@ -1718,7 +1740,11 @@ define(function (require, exports, module) {
17181740
// scanning, assume we are in the middle of a selector. For a preprocessor
17191741
// document we also need to collect the current selector if the cursor is
17201742
// within the selector or whitespaces immediately before or after it.
1721-
if ((!selector || isPreprocessorDoc) && foundChars) {
1743+
// However, if the backward scan encountered a ; before any selector content
1744+
// and the cursor is on an empty/whitespace-only line, we are between
1745+
// statements (e.g. after @import;), not inside a selector.
1746+
if ((!selector || isPreprocessorDoc) && foundChars &&
1747+
(!encounteredSemicolon || _hasNonWhitespace(cm.getLine(pos.line)))) {
17221748
// scan forward to see if the cursor is in a selector
17231749
while (true) {
17241750
if (ctx.token.type !== "comment") {

src/nls/root/strings.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ define({
377377
"LIVE_DEV_CB_WEIGHT_SEMI_BOLD": "Semi Bold",
378378
"LIVE_DEV_CB_WEIGHT_EXTRA_BOLD": "Extra Bold",
379379
"LIVE_DEV_CB_GO_TO_SOURCE": "Go to source",
380+
"LIVE_DEV_CB_TIP_AI_STYLE": "Style with AI",
381+
"LIVE_DEV_CB_AI_PLACEHOLDER": "Describe the style you want\u2026",
382+
"LIVE_DEV_CB_AI_SEND": "Send",
383+
"LIVE_DEV_CB_AI_THINKING": "Applying styles\u2026",
380384
"LIVE_DEV_TOAST_NOT_EDITABLE": "Element not editable - generated by script",
381385
"LIVE_DEV_COPY_TOAST_MESSAGE": "Element copied. Use 'Paste' to add it after the selected element",
382386
"LIVE_DEV_TOAST_FIXED_ELEMENT_DISMISSED": "Element doesn't scroll with page - edit boxes hidden",

0 commit comments

Comments
 (0)