server-data.cc 1.57 KB
Newer Older
1 2 3 4 5 6
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/torque/server-data.h"

7 8 9
#include "src/torque/declarable.h"
#include "src/torque/implementation-visitor.h"

10 11 12 13 14 15 16 17 18 19 20 21 22
namespace v8 {
namespace internal {
namespace torque {

DEFINE_CONTEXTUAL_VARIABLE(LanguageServerData)

void LanguageServerData::AddDefinition(SourcePosition token,
                                       SourcePosition definition) {
  Get().definitions_map_[token.source].emplace_back(token, definition);
}

base::Optional<SourcePosition> LanguageServerData::FindDefinition(
    SourceId source, LineAndColumn pos) {
23 24 25 26 27 28
  if (!source.IsValid()) return base::nullopt;

  auto iter = Get().definitions_map_.find(source);
  if (iter == Get().definitions_map_.end()) return base::nullopt;

  for (const DefinitionMapping& mapping : iter->second) {
29 30 31 32 33 34 35
    SourcePosition current = mapping.first;
    if (current.Contains(pos)) return mapping.second;
  }

  return base::nullopt;
}

36 37 38 39 40
void LanguageServerData::PrepareAllDeclarableSymbols() {
  const std::vector<std::unique_ptr<Declarable>>& all_declarables =
      global_context_->declarables_;

  for (const auto& declarable : all_declarables) {
41 42 43
    // Class field accessors and implicit specializations are
    // auto-generated and should not show up.
    if (!declarable->IsUserDefined()) continue;
44 45 46 47 48 49

    SourceId source = declarable->Position().source;
    symbols_map_[source].push_back(declarable.get());
  }
}

50 51 52
}  // namespace torque
}  // namespace internal
}  // namespace v8