当前位置: 首页>>代码示例>>C++>>正文


C++ TranslationUnit::codeCompleteAt方法代码示例

本文整理汇总了C++中TranslationUnit::codeCompleteAt方法的典型用法代码示例。如果您正苦于以下问题:C++ TranslationUnit::codeCompleteAt方法的具体用法?C++ TranslationUnit::codeCompleteAt怎么用?C++ TranslationUnit::codeCompleteAt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TranslationUnit的用法示例。


在下文中一共展示了TranslationUnit::codeCompleteAt方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: test_main

int test_main(int argc, char * argv[])
{
   try
   { 
      // setup log
      initializeStderrLog("coredev", ::core::system::kLogLevelWarning);

      // ignore sigpipe
      Error error = ::core::system::ignoreSignal(::core::system::SigPipe);
      if (error)
         LOG_ERROR(error);

      // write a C++ file
      std::string cpp =
        "#include <string>\n"
        "class X { public:\n"
        "   void test(int y, int x = 10);\n"
        "}\n"
        "void X::test(int y, int x) {}\n"
        "void foobar() {\n"
        "   X x;\n"
        "   x."
        "}";
      std::ofstream ostr("foo.cpp");
      ostr << cpp;
      ostr.close();

      // load libclang
      using namespace libclang;
      std::string diagnostics;
      clang().load(EmbeddedLibrary(), LibraryVersion(3,4,0), &diagnostics);
      if (!clang().isLoaded())
      {
         std::cerr << "Failed to load libclang: " << diagnostics << std::endl;
         return EXIT_FAILURE;
      }

      // create a source index and get a translation unit for it
      SourceIndex sourceIndex;
      TranslationUnit tu = sourceIndex.getTranslationUnit("foo.cpp");
      if (tu.empty())
      {
         std::cerr << "No translation unit foo.cpp" << std::endl;
         return EXIT_FAILURE;
      }

      // code complete
      CodeCompleteResults results = tu.codeCompleteAt("foo.cpp", 8, 6);
      for (unsigned i = 0; i<results.getNumResults(); i++) {
        std::cout << results.getResult(i).getTypedText() << std::endl;
        std::cout << "   " << results.getResult(i).getText() << std::endl;
      }

      return EXIT_SUCCESS;
   }
   CATCH_UNEXPECTED_EXCEPTION
   
   // if we got this far we had an unexpected exception
   return EXIT_FAILURE ;
}
开发者ID:howarthjw,项目名称:rstudio,代码行数:60,代码来源:Main.cpp

示例2: getCppCompletions

Error getCppCompletions(const core::json::JsonRpcRequest& request,
                        core::json::JsonRpcResponse* pResponse)
{
   // get params
   std::string docPath, userText;
   int line, column;
   Error error = json::readParams(request.params,
                                  &docPath,
                                  &line,
                                  &column,
                                  &userText);
   if (error)
      return error;

   // resolve the docPath if it's aliased
   FilePath filePath = module_context::resolveAliasedPath(docPath);

   // get the translation unit and do the code completion
   std::string filename = filePath.absolutePath();
   TranslationUnit tu = rSourceIndex().getTranslationUnit(filename);

   if (!tu.empty())
   {
      std::string lastTypedText;
      json::Array completionsJson;
      boost::shared_ptr<CodeCompleteResults> pResults =
                              tu.codeCompleteAt(filename, line, column);
      if (!pResults->empty())
      {
         // get results
         for (unsigned i = 0; i<pResults->getNumResults(); i++)
         {
            CodeCompleteResult result = pResults->getResult(i);

            // filter on user text if we have it
            if (!userText.empty() &&
                !boost::algorithm::starts_with(result.getTypedText(), userText))
            {
               continue;
            }

            // check whether this completion is valid and bail if not
            if (result.getAvailability() != CXAvailability_Available)
            {
               continue;
            }

            std::string typedText = result.getTypedText();

            // if we have the same typed text then just ammend previous result
            if ((typedText == lastTypedText) && !completionsJson.empty())
            {
               json::Object& res = completionsJson.back().get_obj();
               json::Array& text = res["text"].get_array();
               text.push_back(friendlyCompletionText(result));
            }
            else
            {
               completionsJson.push_back(toJson(result));
            }

            lastTypedText = typedText;
         }
      }

      json::Object resultJson;
      resultJson["completions"] = completionsJson;
      pResponse->setResult(resultJson);
   }
   else
   {
      // set null result indicating this file doesn't support completions
      pResponse->setResult(json::Value());
   }

   return Success();
}
开发者ID:JSA10,项目名称:rstudio,代码行数:77,代码来源:CodeCompletion.cpp


注:本文中的TranslationUnit::codeCompleteAt方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。