Skip to content

Miss on highlight #5369

Closed
Closed

Description

@FMorschel

Describe the bug
If you ask for a highlight it shows every occurrence of that word.

To Reproduce

  1. Clone https://github.com/FMorschel/sdk/
  2. Go to 26241c9e8676b9bff7cef168de49a08b939ea800
  3. Open pkg/analysis_server/lib/src/lsp/handlers/handler_completion.dart
  4. Leave the cursor at the first element (the new var) on line 481.

Expected behavior
For the highlight to be contained to a single variable (or to not show).
element on 506 and 507 as well as at the end of 481 should not be highlighted.

Screenshots
image

Please complete the following information:

Workspace Environment
Dart Code extension: 3.103.20241211
Flutter extension: 3.103.20241202 (activated)

App: Visual Studio Code
App Host: desktop
Version: win 1.96.0

Workspace type: Dart (LSP) (Multiroot)
Workspace name: server2 (Workspace)

Dart (3.7.0-edge.d76f568f0e2280a59b3378bec95a00fee3f4ecbf): C:\Users\felip_0vh5fa6\dart
Flutter (3.27.0): C:\Users\felip_0vh5fa6\.puro\envs\stable\flutter (No device)

Output from 'dart info'

C:\Users\felip_0vh5fa6\dart\bin\dart.exe info

If providing this information as part of reporting a bug, please review the information
below to ensure it only contains things you're comfortable posting publicly.

General info

  • Dart 3.7.0-edge.d76f568f0e2280a59b3378bec95a00fee3f4ecbf (main) (Fri Dec 13 06:29:44 2024 -0800) on "windows_x64"
  • on windows / "Windows 11 Pro" 10.0 (Build 22631)
  • locale is pt-BR

Project info

  • sdk constraint: '>=3.7.0-edge <4.0.0'
  • dependencies: _fe_analyzer_shared, analysis_server_plugin, analyzer, analyzer_plugin, args, collection, convert, crypto, dart_style, dtd, http, json_rpc_2, language_server_protocol, linter, matcher, memory_usage, meta, path, pub_semver, stream_channel, telemetry, unified_analytics, vm_service, watcher, yaml, yaml_edit
  • dev_dependencies: analyzer_utilities, cli_util, lints, logging, test, test_descriptor, test_reflective_loader, web_socket_channel

Process info

Memory CPU Elapsed time Command line
0 MB -- dart.exe
0 MB -- dart.exe
0 MB -- dart.exe
0 MB -- dart.exe
0 MB -- dart.exe
0 MB -- dart.exe
0 MB -- dart.exe
0 MB -- dart.exe

Activity

  1. DanTup commented on Dec 16, 2024

    @DanTup
    Member

    Seems like we're throwing an error here:

    [3:38:50 PM] [Analyzer] [Info] ==> {"jsonrpc":"2.0","id":107,"method":"textDocument/documentHighlight","params":{"textDocument":{"uri":"file:///D%3A/Dev/Google/dart-sdk/sdk/pkg/analysis_server/lib/src/lsp/handlers/handler_completion.dart"},"position":{"line":505,"character":33}},"clientRequestTime":1734363530728}
    [3:38:50 PM] [Analyzer] [Info] <== {"id":107,"jsonrpc":"2.0","error":{"code":-32001,"message":"An error occurred while handling textDocument/documentHighlight request"}}
    
    Unsupported operation: This is not a fragment,
    #0      FunctionElementImpl.enclosingFragment (package:analyzer/src/dart/element/element.dart:5057:5)
    #1      FragmentExtension.withAncestors (package:analysis_server/src/utilities/extensions/element.dart:81:31)
    #2      _SyncStarIterator.moveNext (dart:async-patch/async_patch.dart:595:13)
    #3      _getUnitElement (package:analysis_server/src/protocol_server.dart:443:49)
    #4      newLocation_fromElement (package:analysis_server/src/protocol_server.dart:306:21)
    #5      convertElement (package:analysis_server/plugin/protocol/protocol_dart.dart:35:15)
    #6      addDartOccurrences.<anonymous closure> (package:analysis_server/src/domains/analysis/occurrences_dart.dart:18:34)
    #7      _LinkedHashMapMixin.forEach (dart:_compact_hash:756:13)
    #8      addDartOccurrences (package:analysis_server/src/domains/analysis/occurrences_dart.dart:16:27)
    #9      DocumentHighlightsHandler.handle.<anonymous closure> (package:analysis_server/src/lsp/handlers/handler_document_highlights.dart:49:7)
    #10     ErrorOrRecord2Extension.mapResults (package:analysis_server/src/lsp/error_or.dart:134:19)
    #11     DocumentHighlightsHandler.handle (package:analysis_server/src/lsp/handlers/handler_document_highlights.dart:47:27)
    

    Expanding the exception:

    // Local functions cannot be augmented.
        throw UnsupportedError(
            'This is not a fragment (tried to get enclosingFragment for $this ($runtimeType), but found $enclosingElement3 (${enclosingElement3.runtimeType}))');
    

    Gives:

    This is not a fragment (tried to get enclosingFragment for ErrorOr<int> (LineInfo lineInfo) (FunctionElementImpl), but found ErrorOr<int> offset (LocalVariableElementImpl))
    

    The code triggering it is this mapResultsSync function inside a method:

    @override
      Future<ErrorOr<CompletionList>> handle(
        CompletionParams params,
        MessageInfo message,
        CancellationToken token,
      ) async {
      // ...
    
        var offset = lineInfo.mapResultSync((lineInfo) => toOffset(lineInfo, pos));
    
  2. added
    in editorRelates to code editing or language features
    in lsp/analysis serverSomething to be fixed in the Dart analysis server
    relies on sdk changesSomething that requires changes in the Dart/Flutter SDK to ship before it will become available
    on Dec 16, 2024
  3. added this to the v3.104.0 milestone on Dec 16, 2024
  4. DanTup commented on Dec 18, 2024

    @DanTup
    Member

    This is fixed by dart-lang/sdk@9820487:

    image

    (Fix ships in a future SDK release, not Dart-Code!)

  5. FMorschel commented on Dec 29, 2024

    @FMorschel
    ContributorAuthor

    Just as a reference, we also have #4454 about highlights to fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in editorRelates to code editing or language featuresin lsp/analysis serverSomething to be fixed in the Dart analysis serveris bugrelies on sdk changesSomething that requires changes in the Dart/Flutter SDK to ship before it will become available

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions