简体   繁体   English

我的 .ycm_extra_conf.py 做错了什么?

[英]What am I doing wrong with my .ycm_extra_conf.py?

I have successfully installed the YouCompleteMe plugin for vim and it is already working, however, I cannot get it to work on an actual project.我已经成功安装了适用于 vim 的 YouCompleteMe 插件并且它已经在工作,但是,我无法让它在实际项目中工作。 For some reason I cannot create a .ycm_extra_conf.py that actually works.出于某种原因,我无法创建实际有效的.ycm_extra_conf.py To test it out I tried making a minimal project, ie I created ~/project with the following structure:为了测试它,我尝试制作一个最小的项目,即我创建了~/project具有以下结构:

~/project
~/project/.ycm_extra_conf.py
~/project/code
~/project/code/example.cpp
~/project/include
~/project/include/example.hpp

Nothing fancy.没有什么花哨。 When I edit example.cpp I try to #include "example.hpp" but it already fails there because ycm says it cannot find the file ( 'example.hpp' file not found [pp_file_not_found] ).当我编辑example.cpp时,我尝试#include "example.hpp"但它已经在那里失败了,因为 ycm 说它找不到文件( 'example.hpp' file not found [pp_file_not_found] )。 vim does find the .ycm_extra_conf.py file and it tries to load it, however, I am not sure whether it's working correctly. vim 确实找到了.ycm_extra_conf.py文件并尝试加载它,但是,我不确定它是否正常工作。

The output from :YcmDebugInfo is: :YcmDebugInfo的输出是:

Printing YouCompleteMe debug information...
-- Resolve completions: Up front
-- Client logfile: /tmp/ycm_bgv1ytg4.log
-- Server Python interpreter: /usr/bin/python3
-- Server Python version: 3.10.4
-- Server has Clang support compiled in: True
-- Clang version: clang version 13.0.0 (https://github.com/ycm-core/llvm 02d1fa3185f1beeebb182b8911bfe09d3429707f)
-- Extra configuration file found and loaded
-- Extra configuration path: ~/project/.ycm_extra_conf.py
-- C-family completer debug information:
--   Clangd running
--   Clangd process ID: 58005
--   Clangd executable: ['~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd', '-header-insertion-decorators=0', '-resource-dir=~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_part
y/clang/lib/clang/13.0.0', '-limit-results=500']
--   Clangd logfiles:
--     /tmp/clangd_stderr2sp81d3p.log
--   Clangd Server State: Initialized
--   Clangd Project Directory: ~/project
--   Clangd Settings: {}
--   Clangd Compilation Command: False
-- Server running at: http://127.0.0.1:55029
-- Server process ID: 57976
-- Server logfiles:
--   /tmp/ycmd_55029_stdout_lm0galgv.log
--   /tmp/ycmd_55029_stderr_myqhxgqt.log

My .ycm_extra_config.py looks like this:我的.ycm_extra_config.py看起来像这样:

import os
import ycm_core

flags = [
  '-Wall',
  '-O3',
  '-I../include',
  '-I./include',
  ]

def FlagsForFile(filename, ** kwargs):
  return
  {
    'flags': flags,
    'do_cache': True
  }

The output from the temporary log files is as follows.临时日志文件的输出如下。 For /tmp/clangd_stderr2sp81d3p.log :对于/tmp/clangd_stderr2sp81d3p.log

I[16:52:02.713] clangd version 13.0.0 (https://github.com/ycm-core/llvm 02d1fa3185f1beeebb182b8911bfe09d3429707f)
I[16:52:02.713] Features: linux
I[16:52:02.713] PID: 58005
I[16:52:02.713] Working directory: ~/project
I[16:52:02.713] argv[0]: ~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd
I[16:52:02.713] argv[1]: -header-insertion-decorators=0
I[16:52:02.713] argv[2]: -resource-dir=~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/13.0.0
I[16:52:02.713] argv[3]: -limit-results=500
I[16:52:02.713] Starting LSP over stdin/stdout
I[16:52:02.714] <-- initialize(1)
I[16:52:02.715] --> reply:initialize(1) 1 ms
I[16:52:02.716] <-- initialized
I[16:52:02.717] <-- workspace/didChangeConfiguration
I[16:52:02.717] <-- textDocument/didOpen
I[16:52:02.718] Failed to find compilation database for ~/project/code/example.cpp
I[16:52:02.718] ASTWorker building file ~/project/code/example.cpp version 1 with command clangd fallback
[~/project/code]
/usr/bin/clang -resource-dir=~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/13.0.0 -- ~/project/code/example.cpp
I[16:52:03.018] --> textDocument/publishDiagnostics

The other one is:另一个是:

2022-06-08 16:52:01,284 - INFO - Completion config: 50, detailing -1 candiates
2022-06-08 16:52:01,284 - INFO - Completion config: 50, detailing -1 candiates
2022-06-08 16:52:01,285 - INFO - Completion config: 50, detailing -1 candiates
2022-06-08 16:52:01,286 - INFO - Completion config: 50, detailing -1 candiates
127.0.0.1 - - [08/Jun/2022 16:52:01] "GET /ready HTTP/1.1" 200 4

2022-06-08 16:52:01,382 - INFO - Clangd executable found at ~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin and up to date
2022-06-08 16:52:01,383 - INFO - Using Clangd from ~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd
2022-06-08 16:52:01,383 - INFO - Computed Clangd command: ['~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd', '-header-insertion-decorators=0', '-resource-dir=~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/13.0.0', '-limit-results=500']
2022-06-08 16:52:01,383 - INFO - Completion config: 50, detailing -1 candiates
2022-06-08 16:52:01,383 - INFO - Returning cached Clangd command: ['~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd', '-header-insertion-decorators=0', '-resource-dir=~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/13.0.0', '-limit-results=500']
127.0.0.1 - - [08/Jun/2022 16:52:01] "GET /signature_help_available?subserver=cpp HTTP/1.1" 200 23
127.0.0.1 - - [08/Jun/2022 16:52:01] "POST /event_notification HTTP/1.1" 200 2
Traceback (most recent call last):
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/bottle/bottle.py", line 868, in _handle
    return route.call(**args)
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/bottle/bottle.py", line 1748, in wrapper
    rv = callback(*a, **ka)
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/watchdog_plugin.py", line 97, in wrapper
    return callback( *args, **kwargs )
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/hmac_plugin.py", line 62, in wrapper
    body = callback( *args, **kwargs )
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/handlers.py", line 63, in EventNotification
    response_data = getattr( _server_state.GetFiletypeCompleter( filetypes ),
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/completers/language_server/language_server_completer.py", line 1787, in OnFileReadyToParse
    self._StartAndInitializeServer( request_data )
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/completers/language_server/language_server_completer.py", line 1773, in _StartAndInitializeServer
    self._extra_conf_dir = self._GetSettingsFromExtraConf( request_data )
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/completers/language_server/language_server_completer.py", line 1741, in _GetSettingsFromExtraConf
    module = extra_conf_store.ModuleForSourceFile( request_data[ 'filepath' ] )
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/extra_conf_store.py", line 55, in ModuleForSourceFile
    return Load( ModuleFileForSourceFile( filename ) )
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/extra_conf_store.py", line 66, in ModuleFileForSourceFile
    if Load( module_file ):
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/extra_conf_store.py", line 151, in Load
    if not force and not _ShouldLoad( module_file, is_global ):
  File "~/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/extra_conf_store.py", line 134, in _ShouldLoad
    raise UnknownExtraConf( module_file )


(Question can be turned off with options, see YCM docs)
127.0.0.1 - - [08/Jun/2022 16:52:01] "POST /event_notification HTTP/1.1" 500 2671
127.0.0.1 - - [08/Jun/2022 16:52:01] "POST /semantic_completion_available HTTP/1.1" 200 4
127.0.0.1 - - [08/Jun/2022 16:52:02] "POST /load_extra_conf_file HTTP/1.1" 200 4


2022-06-08 16:52:02,704 - INFO - Starting Clangd: ['~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd', '-header-insertion-decorators=0', '-resource-dir=~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/13.0.0', '-limit-results=500']
2022-06-08 16:52:02,706 - INFO - Clangd started with PID 58005
127.0.0.1 - - [08/Jun/2022 16:52:02] "POST /event_notification HTTP/1.1" 200 2
2022-06-08 16:52:02,716 - INFO - cfamily: Language server does not require resolve request
2022-06-08 16:52:02,716 - INFO - cfamily: Language server requires sync type of Incremental
2022-06-08 16:52:02,716 - INFO - cfamily: Using characters for signature triggers: (,,
127.0.0.1 - - [08/Jun/2022 16:52:02] "POST /receive_messages HTTP/1.1" 200 4
127.0.0.1 - - [08/Jun/2022 16:52:03] "POST /receive_messages HTTP/1.1" 200 1472
127.0.0.1 - - [08/Jun/2022 16:52:05] "POST /debug_info HTTP/1.1" 200 960
127.0.0.1 - - [08/Jun/2022 16:52:13] "POST /receive_messages HTTP/1.1" 200 4

Any help would be appreciated!任何帮助,将不胜感激!

Cheers干杯

I found the answer.我找到了答案。 Basically my .ycm_extra_conf.py was borked.基本上我的.ycm_extra_conf.py很糟糕。 This one should work:这个应该工作:

import os

flags = [
  '-Wall',
  '-O3',
  '-I../include',
  '-I./include',
  ]


def DirectoryOfThisScript():
  return os.path.dirname(os.path.abspath(__file__))


def Settings(** kwargs):
  return {
    'flags': flags,
    'include_paths_relative_to_dir': DirectoryOfThisScript()
  }

Basically the culprit was基本上罪魁祸首是

return
{

instead of代替

return {

The latter one works, the former doesn't.后者有效,前者无效。

In most cases, for most projects, we recommend:在大多数情况下,对于大多数项目,我们建议:

  1. Using clangd使用 clangd
  2. Creating a compilation database创建编译数据库
  3. NOT using a .ycm_extra_conf.py file at all.根本使用.ycm_extra_conf.py文件。

This gives the best overall experience, including full-project analysis from clangd.这提供了最佳的整体体验,包括来自 clangd 的完整项目分析。

https://github.com/ycm-core/YouCompleteMe#compile-flags https://github.com/ycm-core/YouCompleteMe#compile-flags

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM