Creating multiple selections in Live Preview sets incorrect selection boundaries

When setting multiple selections via the Obsidian API, the selection boundaries don’t work as expected for characters that are hidden by Live Preview.


Steps to reproduce

  1. Create a document with some content that would be hidden by Live Preview (e.g. headings or checkboxes):
    ### Test 1
    ### Test 2
    ### Test 3
    
  2. Create a new plugin and register a command with a new editorCallback. Inside the callback, programmatically select all occurrences of ### via editor.setSelections. For instance:
    editor.setSelections([
      {
        anchor: {
          line: 0,
          ch: 0,
        },
        head: {
          line: 0,
          ch: 3,
        },
      },
      {
        anchor: {
          line: 1,
          ch: 0,
        },
        head: {
          line: 1,
          ch: 3,
        },
      },
      {
        anchor: {
          line: 2,
          ch: 0,
        },
        head: {
          line: 2,
          ch: 3,
        },
      },
    ]);
    
  3. Ensure ‘Source mode’ is disabled. Run the command from step 2.

Expected result

All occurrences have the correct selection boundaries (in the above example, characters 0-3 on each line).

Actual result

Only the first occurrence is fully selected; the other lines only get a cursor at the beginning.

Environment

  • Operating system: macOS
  • Debug info:
    Obsidian version: v1.1.9
    Installer version: v1.1.9
    Operating system: Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64 21.6.0
    Login status: logged in
    Catalyst license: none
    Insider build toggle: off
    Live preview: on
    Legacy editor: off
    Base theme: dark
    Community theme: none
    Snippets enabled: 0
    Restricted mode: off
    Plugins installed: 6
    Plugins enabled: 1
        1: Plugin Testing Sandbox v1.0.0
    

Additional information

You can use this file to reproduce: obsidian-plugin-sandbox/main.ts at live-preview-multi-select · timhor/obsidian-plugin-sandbox · GitHub

The multi-selection works fine when Source mode is on.

1 Like