Vim Tips Wiki
m (Highlight simple python syntax errors moved to Highlight simple Python syntax errors: Page moved by JohnBot to improve title)
m (Reverted edits by Emprego.curitiba (talk | block) to last version by JohnBot)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=969
 
|id=969
  +
|previous=968
|title=Highlight simple python syntax errors
 
  +
|next=970
|created=August 10, 2005 6:54
+
|created=August 10, 2005
 
|complexity=basic
 
|complexity=basic
 
|author=Jon Marshall
 
|author=Jon Marshall
 
|version=6.0
 
|version=6.0
 
|rating=20/14
 
|rating=20/14
  +
|category1=Python
|text=
 
  +
|category2=Syntax
If you want simple syntax errors highlighted in Python (such as if statements with a missing colon at the end) then do the following:
 
 
}}
 
If you want simple syntax errors highlighted in Python (such as if statements with a missing colon at the end) then do the following:
   
# Download and install {{Script|id=790}}
+
# Download and install {{Script|id=790}}.
   
# Search for the line beginning "syn match pythonError". Add the following lines after that:
+
# Search for the line beginning "syn match pythonError". Add the following lines after that:
syn match pythonError "^\s*def\s\+\w\+(.*)\s*$" display
 
syn match pythonError "^\s*class\s\+\w\+(.*)\s*$" display
 
syn match pythonError "^\s*for\s.*[^:]$" display
 
syn match pythonError "^\s*except\s*$" display
 
syn match pythonError "^\s*finally\s*$" display
 
syn match pythonError "^\s*try\s*$" display
 
syn match pythonError "^\s*else\s*$" display
 
syn match pythonError "^\s*else\s*[^:].*" display
 
syn match pythonError "^\s*if\s.*[^\:]$" display
 
syn match pythonError "^\s*except\s.*[^\:]$" display
 
syn match pythonError "^\s*while\s.*[^\:]$" display
 
syn match pythonError "^\s*return\s.*:$" display
 
syn match pythonError "&&" display
 
syn match pythonError "||" display
 
syn match pythonError "[;]$" display
 
syn keyword pythonError do
 
   
  +
<pre>
}}
 
 
syn match pythonError "^\s*def\s\+\w\+(.*)\s*$" display
 
syn match pythonError "^\s*class\s\+\w\+(.*)\s*$" display
 
syn match pythonError "^\s*for\s.*[^:]$" display
 
syn match pythonError "^\s*except\s*$" display
 
syn match pythonError "^\s*finally\s*$" display
 
syn match pythonError "^\s*try\s*$" display
 
syn match pythonError "^\s*else\s*$" display
 
syn match pythonError "^\s*else\s*[^:].*" display
 
syn match pythonError "^\s*if\s.*[^\:]$" display
 
syn match pythonError "^\s*except\s.*[^\:]$" display
 
syn match pythonError "^\s*while\s.*[^\:]$" display
 
syn match pythonError "^\s*return\s.*:$" display
 
syn match pythonError "&&" display
 
syn match pythonError "||" display
 
syn match pythonError "[;]$" display
 
syn keyword pythonError do
  +
</pre>
   
== Comments ==
+
==Comments==
It is a bit too simplistic unfortunately.
+
It is a bit too simplistic unfortunately.
  +
 
Constructions like these will be labeled red on the ''if'' line:
  +
  +
<pre>
 
if (field_name == 'summary'
 
and oldChange['field'] == ticketChange['field']):
  +
</pre>
   
Constructions like these will be labeled red on the if line:
 
if (field_name == 'summary'
 
and oldChange['field'] == ticketChange['field']):
 
asmodai--AT--in-nomine.org
 
, September 19, 2005 10:16
 
 
----
 
----
And so it should highlight that line as there is a syntax error! &lt;wink&gt;
+
And so it should highlight that line as there is a syntax error!
   
Point taken about it being simplistic, hence the 'simple' in the title. I find that
+
Point taken about it being simplistic, hence the 'simple' in the title. I find that
*a) such constructs occur relatively infrequently
+
*Such constructs occur relatively infrequently.
*b) you can avoid by placing a superflous '' at the end of the line:
+
*You can avoid by placing a superfluous '\' at the end of the line:
  +
<pre>
if (foo \
+
if (foo \
and bar)
+
and bar)
  +
</pre>
  +
 
Not ideal, but I find that I omit the trailing colon so often that I prefer the odd false positive.
   
Not ideal, but I find that I omit the trailing colon so often that I prefer the odd false positive.
 
 
----
 
----
hmm I also think its really too simple
+
I also think it's really too simple.
I'm using list comprehension and generator quite often. And than constructs like the following appears regularly. (Yes it's taken from one of my current programs)
 
   
 
I'm using list comprehension and generator quite often. And than constructs like the following appears regularly. (Yes it's taken from one of my current programs)
lMax = sum(sum(self.getComp(id1, id2, user) * self.getWeight(id2, user)
 
  +
for id2 in items) / self.getWeight(id1, user)
 
  +
<pre>
for id1 in items)
 
 
lMax = sum(sum(self.getComp(id1, id2, user) * self.getWeight(id2, user)
 
for id2 in items) / self.getWeight(id1, user)
 
for id1 in items)
  +
</pre>
  +
 
But nevertheless I've simplified it a bit.
  +
  +
<pre>
 
syn match pythonError "^\s*\(class\|def\|for\|while\|try\|except\|finally\|if\|elif\|else\)[^\:]*$" display
  +
syn match pythonError "^\s*\(class\|def\|for\|while\|try\|except\|finally\|if\|elif\|else\)$" display
 
syn match pythonError "&&" display
 
syn match pythonError "||" display
 
syn match pythonError "[;]$" display
 
syn keyword pythonError do
  +
</pre>
  +
 
Unfortunately it doesn't work when the line contains no leading spaces.
  +
  +
----
  +
I prefer this version of 'if':
   
  +
<pre>
But nevertheless I've simplified it a bit.
 
syn match pythonError "^\s*\(class\|def\|for\|while\|try\|except\|finally\|if\|elif\|else\)[^\:]*$" display
+
syn match pythonError "^\s*\(if\|elif\)[^:]*$" display
  +
</pre>
syn match pythonError "^\s*\(class\|def\|for\|while\|try\|except\|finally\|if\|elif\|else\)$" display
 
syn match pythonError "&amp;&amp;" display
 
syn match pythonError "||" display
 
syn match pythonError "[;]$" display
 
syn keyword pythonError do
 
   
  +
It works with one-line expressions like "if 1: do_something()".
Unfortunately it doesn't work when the line contains no leading spaces.
 
   
  +
Apart from that, works well for me as I keep forgetting the ":" often. Exactly what I was looking for.
Maybe there is somebody able to help?
 
   
 
----
 
----
<!-- parsed by vimtips.py in 0.528713 seconds-->
 
[[Category:Python]]
 
[[Category:Syntax]]
 

Latest revision as of 14:49, 8 November 2011

Tip 969 Printable Monobook Previous Next

created August 10, 2005 · complexity basic · author Jon Marshall · version 6.0


If you want simple syntax errors highlighted in Python (such as if statements with a missing colon at the end) then do the following:

  1. Download and install script#790.
  1. Search for the line beginning "syn match pythonError". Add the following lines after that:
syn match pythonError "^\s*def\s\+\w\+(.*)\s*$" display
syn match pythonError "^\s*class\s\+\w\+(.*)\s*$" display
syn match pythonError "^\s*for\s.*[^:]$" display
syn match pythonError "^\s*except\s*$" display
syn match pythonError "^\s*finally\s*$" display
syn match pythonError "^\s*try\s*$" display
syn match pythonError "^\s*else\s*$" display
syn match pythonError "^\s*else\s*[^:].*" display
syn match pythonError "^\s*if\s.*[^\:]$" display
syn match pythonError "^\s*except\s.*[^\:]$" display
syn match pythonError "^\s*while\s.*[^\:]$" display
syn match pythonError "^\s*return\s.*:$" display
syn match pythonError "&&" display
syn match pythonError "||" display
syn match pythonError "[;]$" display
syn keyword pythonError do

Comments[]

It is a bit too simplistic unfortunately.

Constructions like these will be labeled red on the if line:

if (field_name == 'summary'
  and oldChange['field'] == ticketChange['field']):

And so it should highlight that line as there is a syntax error!

Point taken about it being simplistic, hence the 'simple' in the title. I find that

  • Such constructs occur relatively infrequently.
  • You can avoid by placing a superfluous '\' at the end of the line:
  if (foo \
  and bar)

Not ideal, but I find that I omit the trailing colon so often that I prefer the odd false positive.


I also think it's really too simple.

I'm using list comprehension and generator quite often. And than constructs like the following appears regularly. (Yes it's taken from one of my current programs)

lMax = sum(sum(self.getComp(id1, id2, user) * self.getWeight(id2, user)
for id2 in items) / self.getWeight(id1, user)
for id1 in items)

But nevertheless I've simplified it a bit.

syn match pythonError "^\s*\(class\|def\|for\|while\|try\|except\|finally\|if\|elif\|else\)[^\:]*$" display
syn match pythonError "^\s*\(class\|def\|for\|while\|try\|except\|finally\|if\|elif\|else\)$" display
syn match pythonError "&&" display
syn match pythonError "||" display
syn match pythonError "[;]$" display
syn keyword pythonError do

Unfortunately it doesn't work when the line contains no leading spaces.


I prefer this version of 'if':

syn match pythonError "^\s*\(if\|elif\)[^:]*$" display

It works with one-line expressions like "if 1: do_something()".

Apart from that, works well for me as I keep forgetting the ":" often. Exactly what I was looking for.