Vim: Error format matches everything
Asked Answered
B

4

6

I am trying to match the following error with efm:

AssertionError: 1 == 2
    at /home/strager/projects/blah/blah.js:13:37

The error message can be anything (i.e. it doesn't always match the AssertionError: .* or .*Error: .* formats). The general format is:

errormessage
    at filename:line:column

My problem is that the error message matches any line; I need to restrict the error message to one line only, and only match if it's followed by a matching "at" line.

I have tried the following efm:

set efm=%Z\ \ \ \ at\ %f:%l:%c,%E%m
" %Z    at %f:%l:%c,%E%m

This almost works, but it matches status lines (e.g. non-errors before and after the error) in addition to the errors. How can I force %E%m ... %Z to be only two lines total (one for the error message, and one for the at line)? I have access to the standard UNIX tools for makeprg if needed.

Balsa answered 15/9, 2010 at 18:14 Comment(0)
G
1

Do you really want to spend your time learning an obscure pattern language that has no application anywhere else? Unless someone is paying you to write vim compiler plug-ins, I wouldn't (and I love vim!). Since you're willing to open the toolbox, just write a wrapper around your checker and spit out an easy-to-parse format. Eg:

#!/usr/bin/perl
use warnings;
use strict;

open my $fh, '-|', 'compiler', @_ or die $!;

my $last_line = <$fh> // exit;
while (defined(my $line = <$fh>)) {
    my($file, $l, $c) = $line =~ /^    at (.+?):(\d+):(\d+)$/;
    print "$file:$l:$c: $last_line" if defined($file);
    $last_line = $line;
}
Grimonia answered 17/9, 2010 at 21:19 Comment(4)
How to use this perl script in vim?Feeding
Just :set makeprg=.... That is, whatever program you were using as makeprg, just replace it with this script.Grimonia
I've taken the liberty of replacing -w with use warnings;. The -w switch is pretty much only needed for compatibility with perl 5.5, which didn't support lexical warnings, but your script requires 5.6 anyway because of the open syntax used. (Actually it requires 5.10+ because of //.)Ante
Your code only works if compiler writes error messages to stdout, not stderr.Ante
F
1

Does this work?

set efm=%Z\ \ \ \ at\ %f:%l:%c,%E%m,%-G%.%#

The %-G%.%# tells vim to ignore entire lines that do not match the other patterns.

Falsetto answered 26/1, 2011 at 18:5 Comment(0)
F
0

What about ...

set efm=%E%m,%Z\ \ \ \ at\ %f:%l:%c
Fiddlestick answered 15/9, 2010 at 22:58 Comment(1)
This is what I had initially, but this matches every line as an "error" with no more information. The efm I posted in my question does this but also detects the file name and line and column numbers of the "at" line.Balsa
A
0

Maybe

set efm=%E%>%m,%Z\ \ \ \ at\ %f:%l:%c

Check this

:help efm%>
Anonymous answered 2/11, 2018 at 17:16 Comment(1)
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Gobetween

© 2022 - 2024 — McMap. All rights reserved.