@@ -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" ) {
0 commit comments