tek*_*nix 19 c++ vim syntax-highlighting syntax-error
是否有可能让gVim像Visual Studio一样实时突出显示C++语法错误(下面的红色曲线)?
VIM对我来说好多年了,但是当我意识到这个编辑器有多么有用以及用更多功能扩展这个编辑器是多么容易时,我转到了Sublime Text 3.
现在,我在Sublime Text中编辑并在同一个应用程序中编译.我已经制作了一个语法高亮显示器,以更好的方式显示错误,如果您单击错误,它会将您带到发生错误的位置.
按照这些步骤,用c ++编写代码比以前简单得多.
所以在安装了sublime_text并在获得文件夹后运行它~/HOME/.config/sublime-text-3/
.如果您不熟悉sublime_text,则可以说您可以将修改添加到此文件夹中
~/HOME/.config/sublime-text-3/Packages/User
.我们$SUBLIME_CONFIG_DIR
从现在开始调用此文件夹.我将在这里向您展示如何为C++添加构建系统以及如何语法突出显示输出.
通过添加名为c++build.sublime-build
to 的文件来创建您的构建系统,$SUBLIME_CONFIG_DIR
其中包含以下内容:
{
"shell" : true,
"cmd": ["make $file_base_name"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"selector": "source.c++",
"working_dir": "${file_path}",
"syntax" : "Packages/User/c++output.tmLanguage"
}
Run Code Online (Sandbox Code Playgroud)
我将解释每一行的作用.1. "shell":true
简单地说如果你在文件上调用build,sublime应该运行一个shell命令.2. cmd
将在调用构建时执行.您可以使用g ++或其他任何东西而不是make.我在这里放置的这个构建配置是一个起点,你可以修改它并让它做你想做的.3. selector
告诉sublime哪些文件将自动使用此版本(在本例中为所有c ++文件)4.workig_dir
我将其设置为当前目录以使其更容易5. syntax
当您构建文件时,输出将在输出视图中显示为纯文本没有语法突出显示.在这里,我将使用C++output.tmLanguage
文件,我将在稍后解释,它可以帮助您获得更好的突出显示输出,以便于调试.6. file_regex
匹配输出中的错误行,如果有错误,双击错误会将您带到相应的文件.
有各种方法可以将新语法高亮显示添加到sublime.您可以使用JSON然后将其转换为PList,您可以直接使用PList.为简单起见,只需在名为的文件中复制以下内容c++output.tmLanguage
.在启动应用程序时,Sublime Text会自动选取tmLanguage文件以突出显示文件.
内容应如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>fileTypes</key>
<array>
<string>ssraw</string>
</array>
<key>name</key>
<string>Mazanin</string>
<key>patterns</key>
<array>
<dict>
<key>match</key>
<string>\b(error)\b</string>
<key>name</key>
<string>invalid.illegal</string>
</dict>
<dict>
<key>match</key>
<string>(warning|instantiation|note|required|candidate)</string>
<key>name</key>
<string>markup.quote</string>
</dict>
<dict>
<key>match</key>
<string>^.*:[0-9]+</string>
<key>name</key>
<string>support.variable.mazanin</string>
</dict>
<dict>
<key>begin</key>
<string>\[</string>
<key>beginCaptures</key>
<dict>
<key>0</key>
<dict>
<key>name</key>
<string>punctuation.definition.mazanin</string>
</dict>
</dict>
<key>end</key>
<string>\]</string>
<key>endCaptures</key>
<dict>
<key>0</key>
<dict>
<key>name</key>
<string>punctuation.definition.mazanin</string>
</dict>
</dict>
<key>name</key>
<string>comment.mazanin</string>
<key>patterns</key>
<array>
<dict>
<key>match</key>
<string>\\.</string>
<key>name</key>
<string>source.mazanin</string>
</dict>
</array>
</dict>
<dict>
<key>begin</key>
<string>\(</string>
<key>beginCaptures</key>
<dict>
<key>0</key>
<dict>
<key>name</key>
<string>punctuation.definition.mazanin</string>
</dict>
</dict>
<key>end</key>
<string>\)</string>
<key>endCaptures</key>
<dict>
<key>0</key>
<dict>
<key>name</key>
<string>punctuation.definition.mazanin</string>
</dict>
</dict>
<key>name</key>
<string>storage.mazanin</string>
<key>patterns</key>
<array>
<dict>
<key>match</key>
<string>\\.</string>
<key>name</key>
<string>source.mazanin</string>
</dict>
</array>
</dict>
</array>
<key>scopeName</key>
<string>source.cerr</string>
<key>uuid</key>
<string>ca03e751-04ef-4330-9a6b-9b99aae1c418</string>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)
请记住用唯一的uuid替换上面的uuid(字符串).但你怎么得到一个.打开sublime-console并输入以下内容:
import uuid
uuid.uuid4()
Run Code Online (Sandbox Code Playgroud)
现在你基本上完成了.打开你的c ++文件,调用build,你应该能够看到突出显示的错误并点击如下(我使用Dawn主题).
我个人更喜欢包装输出错误的行并快速打开它们,所以我在我的keymappings中添加了这个快捷方式,$SUBLIME_CONFIG_DIR/Default (Linux).sublime-keymap
其中说:
[
{
"keys": ["ctrl+shift+l"], "command": "toggle_setting", "args": {"setting": "word_wrap"}
}
]
Run Code Online (Sandbox Code Playgroud)
现在,如果您预先设置,ctrl+shift+l
您可以简单地打包/解包输出.在大多数情况下,当错误很长且信息没有帮助时,这更有用.上面没有包装的例子看起来像:
您还可以使用YAML来描述语法突出显示规则,这些规则更简洁,更易于修改.但是,您需要PackageDev
在sublime上安装才能使用YAML语言.将以下文件放入您的文件中$HOME/.config/sublime-text-3/Packages/User
并使用sublime打开它.按F7
,将为您生成语法文件.
# [PackageDev] target_format: plist, ext: tmLanguage
---
name: C++ Error Output
scopeName: source.boo
fileTypes: [boo]
uuid: 45319b4d-90f8-4ff1-9a66-c56ed5c408a4
patterns:
- include: '#pars'
- include: '#bracs'
- include: '#anglebracs'
- include: '#quotes'
- include: '#curlies'
- match: \b((e|E)rror)\b
name: invalid.illegal
- match: (warning|instantiation|note|required|candidate)
name: markup.quote
- match: ^[^\:\s]*(?=:)
name: support.variable
- match: (?<=:)[0-9]+
name: keyword.control
repository:
bracs:
name: markup.quote
begin: \[
beginCaptures:
'0': {name: keyword}
end: \]
endCaptures:
'0': {name: keyword}
patterns:
- include: $self
- include: anglebracs
- include: pars
pars:
name: variable.parameter
begin: \(
beginCaptures:
'0': {name: keyword}
end: (\)|$)
endCaptures:
'0': {name: keyword}
patterns:
- include: $self
- include: anglebracs
anglebracs:
name: markup.raw
begin: (?<!<)\<(?!\<)
beginCaptures:
'0': {name: keyword}
end: \>
endCaptures:
'0': {name: keyword}
patterns:
- include: $self
- include: pars
quotes:
name: markup.heading
begin: ‘
beginCaptures:
'0': {name: keyword}
end: ’
endCaptures:
'0': {name: keyword}
patterns:
- include: $self
- include: anglebracs
- include: pars
- include: bracs
curlies:
name: markup.list
begin: \{
beginCaptures:
'0': {name: keyword}
end: \}
endCaptures:
'0': {name: keyword}
patterns:
- include: $self
- include: anglebracs
- include: pars
- include: bracs
...
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到颜色名称列表