<commit_before>// @flow

import {LanguageClientConnection, type Location, type ServerCapabilities} from '../languageclient';
import Convert from '../convert';

export default class NuclideFindReferencesAdapter {
  static canAdapt(serverCapabilities: ServerCapabilities): boolean {
    return serverCapabilities.referencesProvider === true;
  }

  async getReferences(connection: LanguageClientConnection, editor: atom$TextEditor, point: atom$Point, projectRoot: ?string): Promise<?nuclide$FindReferencesReturn> {
    const documentPositionParams = {
      textDocument: Convert.editorToTextDocumentIdentifier(editor),
      position: Convert.pointToPosition(point),
      context: {includeDeclaration: true}
    };

    const locations = await connection.findReferences(documentPositionParams);
    if (locations == null || locations.length === 0) { return null; }

    const references = locations.map(NuclideFindReferencesAdapter.locationToReference);

    return {
      type: 'data',
      baseUri: projectRoot || '',
      referencedSymbolName: NuclideFindReferencesAdapter.getReferencedSymbolName(editor, point, references),
      references,
    };
  }

  static locationToReference(location: Location): nuclide$Reference {
    return {
      uri: Convert.uriToPath(location.uri),
      name: null,
      range: Convert.lsRangeToAtomRange(location.range),
    };
  }

  static getReferencedSymbolName(editor: atom$TextEditor, point: atom$Point, references: Array<nuclide$Reference>): string {
    const currentReference = references.find(r => r.range.containsPoint(point)) || references[0];
    return editor.getBuffer().getTextInRange(currentReference.range);
  }
}<commit_msg>[find-references] When there's no references, return an empty array<commit_after>// @flow

import {LanguageClientConnection, type Location, type ServerCapabilities} from '../languageclient';
import Convert from '../convert';

export default class NuclideFindReferencesAdapter {
  static canAdapt(serverCapabilities: ServerCapabilities): boolean {
    return serverCapabilities.referencesProvider === true;
  }

  async getReferences(connection: LanguageClientConnection, editor: atom$TextEditor, point: atom$Point, projectRoot: ?string): Promise<?nuclide$FindReferencesReturn> {
    const documentPositionParams = {
      textDocument: Convert.editorToTextDocumentIdentifier(editor),
      position: Convert.pointToPosition(point),
      context: {includeDeclaration: true}
    };

    const locations = await connection.findReferences(documentPositionParams);
    if (locations == null) { return null; }

    const references = locations.map(NuclideFindReferencesAdapter.locationToReference);

    return {
      type: 'data',
      baseUri: projectRoot || '',
      referencedSymbolName: NuclideFindReferencesAdapter.getReferencedSymbolName(editor, point, references),
      references,
    };
  }

  static locationToReference(location: Location): nuclide$Reference {
    return {
      uri: Convert.uriToPath(location.uri),
      name: null,
      range: Convert.lsRangeToAtomRange(location.range),
    };
  }

  static getReferencedSymbolName(editor: atom$TextEditor, point: atom$Point, references: Array<nuclide$Reference>): string {
    if (references.length === 0) {
      return '';
    }
    const currentReference = references.find(r => r.range.containsPoint(point)) || references[0];
    return editor.getBuffer().getTextInRange(currentReference.range);
  }
}
{'fraction_non_alphanumeric': 0.10648148148148148, 'fraction_numerical': 0.0011574074074074073, 'mean_word_length': 4.859322033898305, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3218216', 'n_tokens_mistral': 926, 'n_tokens_neox': 884, 'n_words': 198}