Class GrammarReferenceLocator

java.lang.Object
org.eclipse.lemminx.extensions.index.GrammarReferenceLocator

public class GrammarReferenceLocator extends Object
Locates grammar file references in XML content and returns them as GrammarReference objects with pre-computed line/column positions.

Positions are computed once during locate(TextDocument) from the TextDocument passed in. After that, callers build LSP Locations and TextEdits directly from the cached GrammarReference.line(), GrammarReference.startChar(), and GrammarReference.endChar() — no file re-read required.

This is step 2 in the rename flow (after Bloom filter pre-filtering):

  1. WorkspaceIndex.findReferencingFiles(java.lang.String) — Bloom filter finds candidates (instant)
  2. GrammarReferenceLocator — scans each candidate for exact positions
  3. GrammarFileRenameSupport — builds the WorkspaceEdit from the located refs

Unlike XMLReferenceCollector which collects names into a Set for Bloom filter indexing, this class returns GrammarReference records with line/column positions for building TextEdits.

Use cases covered (Phase 1 — file-level references):

  • UC14: <?xml-model href="..."?> — rename grammar → update href
  • UC15: xsi:noNamespaceSchemaLocation="..." — idem
  • UC16: xsi:schemaLocation="ns path" — update the file path part
  • UC17/18: <!DOCTYPE ... SYSTEM "..."> — update system ID
  • UC21: href="..." and location="..." on XInclude, XSLT import/include, WSDL import
  • xs:include/xs:import schemaLocation="..."
  • OASIS XML Catalog: uri="..." on uri, public, system, systemSuffix, uriSuffix entries

Uses LemMinX's XMLScanner (fault-tolerant, no DOM). All comparisons are char-by-char — Strings are only created for values that are actually returned as GrammarReferences.

See Also:
  • Constructor Details

    • GrammarReferenceLocator

      public GrammarReferenceLocator()
  • Method Details

    • locate

      public static List<GrammarReference> locate(TextDocument doc)
      Scans XML content and returns every grammar reference with its exact line/column position.

      Use case (indexing): WorkspaceIndex.indexFile(java.nio.file.Path) reads a bounded head of order.xml into a TextDocument and passes it here. The returned references — e.g. [XML_MODEL_HREF line=0,col=17-27 "schema.xsd"] — are cached in the FileBloomEntry and used for all subsequent code-lens counts, Find References, and rename edits without re-reading the file.

      Parameters:
      doc - the XML document to scan (may be a head-limited fragment).
      Returns:
      all located grammar references; empty if none found.
    • locate

      public static List<GrammarReference> locate(TextDocument doc, List<IGrammarReferenceContributor> contributors)
      Scans XML content and returns every grammar reference, including those discovered by extension-provided IGrammarReferenceContributors.
      Parameters:
      doc - the XML document to scan.
      contributors - extension-provided contributors for non-standard grammar reference attributes.
      Returns:
      all located grammar references; empty if none found.
    • locate

      public static List<GrammarReference> locate(CharSequence text)
      Convenience wrapper: wraps text in a TextDocument and delegates to locate(TextDocument).
      Parameters:
      text - raw XML content.
      Returns:
      all located grammar references; empty if none found.
    • locate

      public static List<GrammarReference> locate(CharSequence text, List<IGrammarReferenceContributor> contributors)
      Convenience wrapper: wraps text in a TextDocument and delegates to locate(TextDocument, List).
      Parameters:
      text - raw XML content.
      contributors - extension-provided contributors.
      Returns:
      all located grammar references; empty if none found.
    • locateReferencesTo

      public static List<GrammarReference> locateReferencesTo(CharSequence text, String fileName)
      Finds grammar references in text whose filename portion equals fileName.

      Use case: Bloom-filter pre-filtering for rename: quickly find candidate references in order.xml when renaming schema.xsd. A reference href="../schemas/schema.xsd" matches because its last path segment is "schema.xsd". Callers that need path accuracy should use locateReferencesTo(CharSequence, Path, Path) instead.

      Parameters:
      text - XML content to scan.
      fileName - bare filename to match (e.g. "schema.xsd").
      Returns:
      references whose last path segment equals fileName; empty if none.
    • locateReferencesTo

      public static List<GrammarReference> locateReferencesTo(CharSequence text, String fileName, List<IGrammarReferenceContributor> contributors)
    • locateReferencesTo

      public static List<GrammarReference> locateReferencesTo(CharSequence text, Path targetPath, Path referencingFile)
      Finds grammar references in text that actually resolve to targetPath when the containing file is referencingFile.

      This is the path-accurate variant used by Find References and the code lens participant. Without path resolution, two XSD files named foo.xsd in different directories would each claim references that belong to the other.

      Use case: Workspace has schemas/order.xsd and lib/order.xsd. data.xml contains href="schemas/order.xsd". Opening lib/order.xsd must show 0 references; opening schemas/order.xsd must show 1 reference. This method resolves "schemas/order.xsd" relative to data.xml's directory and compares with the target path.

      Parameters:
      text - XML content to scan.
      targetPath - absolute path of the grammar file being queried.
      referencingFile - absolute path of the file being scanned.
      Returns:
      references that resolve to targetPath; empty if none.
    • locateReferencesTo

      public static List<GrammarReference> locateReferencesTo(CharSequence text, Path targetPath, Path referencingFile, List<IGrammarReferenceContributor> contributors)
    • locateReferencesTo

      public static List<GrammarReference> locateReferencesTo(TextDocument doc, Path targetPath, Path referencingFile, List<IGrammarReferenceContributor> contributors)
      Finds grammar references in doc that actually resolve to targetPath when the containing file is referencingFile.

      Use case — rename with open document: When a file is open in the editor and the user has made edits, cached line/column positions may no longer match the live DOM. GrammarFileRenameSupport passes the live document content here to recompute accurate positions before building the rename TextEdits.

      Parameters:
      doc - live document content.
      targetPath - absolute path of the grammar file being renamed.
      referencingFile - absolute path of the file being scanned.
      contributors - extension-provided contributors.
      Returns:
      references that resolve to targetPath; empty if none.
    • locateReferencesWithValue

      public static List<GrammarReference> locateReferencesWithValue(CharSequence text, String identifier)
      Finds grammar references in text whose value exactly equals identifier.

      Used by catalog code lenses to verify that a candidate file actually references a grammar via a catalog key (systemId, publicId, or URI name) rather than by filename. Unlike locateReferencesTo(CharSequence, String), this method does not strip path segments — the full value must match identifier exactly.

      Parameters:
      text - XML content to scan.
      identifier - catalog key to match (e.g. "http://example.org/schema").
      Returns:
      references whose value equals identifier; empty if none.
    • locateReferencesWithValue

      public static List<GrammarReference> locateReferencesWithValue(CharSequence text, String identifier, List<IGrammarReferenceContributor> contributors)
    • locateReferencesWithValue

      public static List<GrammarReference> locateReferencesWithValue(List<GrammarReference> all, String identifier)