叮当完全不适用于未保存的文件
Clang complete not working for unsaved files?
我发现代码完整对于未保存的文件不起作用(通过libclang-c)(但对于保存的文件效果很好)。
代码(不是全部):
// show complete options
void showComplete(CXTranslationUnit TU, char *src_filename, unsigned filesize, CXUnsavedFile *file, int line, int column)
{
fprintf(stderr, "TU=%p, file=%s complete at line=%i, column=%in", &TU, src_filename, line, column);
CXCodeCompleteResults *results =
clang_codeCompleteAt(TU, src_filename, line, column, file, (file == NULL ? 0 : 1), clang_defaultCodeCompleteOptions());
if (results) {
fprintf(stderr, "results=[%p, count=%i]n", results->Results, results->NumResults);
/* Sort the code-completion results based on the typed text. */
clang_sortCodeCompletionResults(results->Results, results->NumResults);
for (int i = 0; i < results->NumResults; i++)
print_completion_result(results->Results + i, stdout);
} else {
fprintf(stderr, "no complete resultsn");
}
clang_disposeCodeCompleteResults(results);
}
int main(int argc, char** argv)
{
std::cout << "Starting ----" << std::endl;
CXIndex index = clang_createIndex(false, false);
char * filename = argv[1];
fprintf(stderr, "ncommand-line file: %s =============n", filename);
CXTranslationUnit commandLineTu = clang_parseTranslationUnit(index, filename, 0, 0, 0, 0, CXTranslationUnit_None);
showComplete(commandLineTu, filename, 0, NULL, 1, 72);
clang_disposeTranslationUnit(commandLineTu);
// unsaved file tu
fprintf(stderr, "nunsaved file =============n");
CXUnsavedFile *unsavedFile = new CXUnsavedFile;
unsavedFile->Filename = "temp.cpp";
unsavedFile->Contents = "struct TempStruct { int a; bool b; }; int main() { TempStruct str; str. /*complete here*/ return 1; }";
CXTranslationUnit in_memory_tu = clang_parseTranslationUnit(index, unsavedFile->Filename, NULL, 0, unsavedFile, 1, CXTranslationUnit_None);
int column = strchr(unsavedFile->Contents, '.') - unsavedFile->Contents + 1 + 1; // + 1 (length of '.') + 1 (next symbol)
showComplete(in_memory_tu, (char*)unsavedFile->Filename, strlen(unsavedFile->Contents), unsavedFile, 1, column);
clang_disposeTranslationUnit(in_memory_tu);
clang_disposeIndex(index);
return 0;
}
源文件(保存的文件,正好等于未保存的文件内容):
struct TempStruct { int a; bool b; }; int main() { TempStruct str; str. /*complete here*/ return 1; }
输出:
开始----命令行文件:../test/test_complete.cpp =============TU=0x7fff5d75aaf0,文件=../test/test_complete.cpp 完成于行=1,列=72结果=[0x7fec7b509510,计数=5]FieldDecl:{ResultType int}{TypedText a} (35) FieldDecl:{ResultType bool}{TypedText b} (35)CXXMethod:{ResultType TempStruct &}{TypedText operator=}{LeftParen(}{Placeholder const tempStruct &}{RightParen )} (34)StructDecl:{TypedText TempStruct}{Text ::} (75)CXXDestructor:{ResultType void}{TypedText ~TempStruct}{LeftParen(}{RightParen )} (34)未保存的文件 =============TU=0x7fff5d75aaf0,文件=temp.cpp完成于行=1,列=72结果=[0x7fec7b5043d0,计数=0]
有什么想法吗?
我忘了设置未保存的文件>长度。即使是源文件也是您必须隐式设置长度的文本文件(例如 unsavedFiles->Length = strlen(unsavedFile->Contents);
)
相关文章:
- 保存文件并使控制台不关闭:Visual Studio 2019
- 将值更改为 c++ 的数组并保存文件
- 如何保存文件中的所有数据?C++
- 更改代码::块上的默认保存文件类型
- 使用setW()函数保存文件时,如何获取每个属性的值
- 尝试使用 ostream 保存文件内容时出错
- 如何使用3d的max 2016 SDK(C++)在插件中添加保存文件对话框?
- 如何在不保存文件的情况下制作打印屏幕并将其发送到FTP服务器?我的工作代码将文件保存到HDD
- 视频编写器功能不会使用 OpenCV-3.0.0 保存文件
- 使用fstream明确保存文件,而无需在C 中关闭文件
- 位换档字符串以解码保存文件C
- QFile在程序退出时保存文件,根本不会保存~500MB文件
- 保存文件或文件夹的时间戳
- Fstream不会保存文件中的最后一个单词,也不会从文件中读取
- CFileDialog保存文件的名称为空
- 如何以正确的编码保存文件
- 在动态分配的 char 数组中保存文件的类内容
- 如何在win32中使用GetSave文件名保存文件
- 覆盖保存文件的非常安全的方法
- 除非先使用MS Paint打开/保存文件,否则带有QRCode位图的LoadImage()将失败