kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/indexer/textproto/plugins/example/plugin.cc (about) 1 /* 2 * Copyright 2020 The Kythe Authors. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "plugin.h" 18 19 #include "kythe/cxx/indexer/proto/vname_util.h" 20 21 namespace kythe { 22 namespace lang_textproto { 23 24 absl::Status ExamplePlugin::AnalyzeStringField( 25 PluginApi* api, const proto::VName& file_vname, 26 const google::protobuf::FieldDescriptor& field, 27 const std::vector<StringToken>& tokens) { 28 // Create an anchor spanning the full range of the string value. 29 const char* begin = tokens.front().source_text.data(); 30 const char* end = tokens.back().source_text.end(); 31 const absl::string_view full_span = absl::string_view(begin, end - begin); 32 const proto::VName anchor_vname = 33 api->CreateAndAddAnchorNode(file_vname, full_span); 34 35 std::string full_value; 36 for (const auto& t : tokens) { 37 full_value += t.parsed_value; 38 } 39 40 LOG(ERROR) << "[Example Plugin] String value:" << full_value; 41 LOG(ERROR) << "[Example Plugin] String value full span: " << full_span; 42 43 // Create a VName for a semantic node representing this person. The person's 44 // name is used as the "signature". 45 proto::VName person_vname; 46 person_vname.set_signature(full_value); 47 person_vname.set_language("textproto_plugin_example"); 48 person_vname.set_corpus(file_vname.corpus()); 49 50 if (field.name() == "name") { 51 LOG(ERROR) << "[Example Plugin] Defined new person:\n" << person_vname; 52 53 api->recorder()->AddProperty(VNameRef(person_vname), NodeKindID::kVariable); 54 api->recorder()->AddEdge(VNameRef(anchor_vname), 55 EdgeKindID::kDefinesBinding, 56 VNameRef(person_vname)); 57 } else if (field.name() == "friend") { 58 LOG(ERROR) << "[Example Plugin] Added ref for friend field:\n" 59 << person_vname; 60 api->recorder()->AddEdge(VNameRef(anchor_vname), EdgeKindID::kRef, 61 VNameRef(person_vname)); 62 } 63 64 return absl::OkStatus(); 65 } 66 67 } // namespace lang_textproto 68 } // namespace kythe