有没有办法立即修复所有MATLAB mlint消息?

Ric*_*ton 11 matlab mlint

我继承了一些代码,其中作者厌恶分号.是否可以一次性修复所有mlint消息(至少所有具有自动修复的消息),而不是必须单击每个消息并按ALT + ENTER?

gno*_*ice 8

注意:此答案使用MLINT函数,在较新版本的MATLAB中不再推荐使用.较新的函数CHECKCODE是首选,下面的代码仍然可以通过调用这个较新的函数简单地替换对MLINT的调用.


我不知道的方式在一般的自动修复基于代码MLINT消息.但是,在您的特定情况下,您可以通过自动方式将分号添加到抛出MLINT警告的行.

首先,让我们从这个示例脚本开始junk.m:

a = 1
b = 2;
c = 'a'
d = [1 2 3]
e = 'hello';
Run Code Online (Sandbox Code Playgroud)

第一行,第三行和第四行将为您提供MLINT警告消息"使用分号终止语句以禁止输出(在脚本中)".使用MLINT的函数形式,我们可以在文件中找到发生此警告的行.然后,我们可以从文件中读取所有代码行,在发生警告的行的末尾添加分号,然后将代码行写回文件.这是执行此操作的代码:

%# Find the lines where a given mlint warning occurs:

fileName = 'junk.m';
mlintID = 'NOPTS';                       %# The ID of the warning
mlintData = mlint(fileName,'-id');       %# Run mlint on the file
index = strcmp({mlintData.id},mlintID);  %# Find occurrences of the warnings...
lineNumbers = [mlintData(index).line];   %#   ... and their line numbers

%# Read the lines of code from the file:

fid = fopen(fileName,'rt');
linesOfCode = textscan(fid,'%s','Delimiter',char(10));  %# Read each line
fclose(fid);

%# Modify the lines of code:

linesOfCode = linesOfCode{1};  %# Remove the outer cell array encapsulation
linesOfCode(lineNumbers) = strcat(linesOfCode(lineNumbers),';');  %# Add ';'

%# Write the lines of code back to the file:

fid = fopen(fileName,'wt');
fprintf(fid,'%s\n',linesOfCode{1:end-1});  %# Write all but the last line
fprintf(fid,'%s',linesOfCode{end});        %# Write the last line
fclose(fid);
Run Code Online (Sandbox Code Playgroud)

现在文件junk.m应该在每行的末尾都有分号.如果需要,可以将上面的代码放在一个函数中,以便您可以在继承的代码的每个文件上轻松运行它.


Mat*_* B. 7

为了以一般方式解决所有可用的自动修复操作的问题,我们必须采用可怕的未记录的java方法.mlint(现在checkcode)的实现使用mlintmex(内置;而不是名称所示的mexfile),它只返回linter的文本输出.没有暴露自动修复; 偶数行和列号以纯文本形式发出.它似乎与Matlab安装中的mlint二进制文件的输出相同($(matlabroot)/bin/$(arch)/mlint)

所以我们必须回到编辑器本身使用的java实现.注意:此处遵循R2013a非常未记载的代码.

%// Get the java component for the active matlab editor
ed = matlab.desktop.editor.getActive().JavaEditor.getTextComponent();
%// Get the java representation of all mlint messages
msgs = com.mathworks.widgets.text.mcode.MLint.getMessages(ed.getText(),ed.getFilename())

%// Loop through all messages and apply the autofix, if it exits 
%// Iterate backwards to try to prevent changing the location of subsequent
%// fixes... but two nearby fixes could still mess each other up.
for i = msgs.size-1:-1:0
  if msgs.get(i).hasAutoFix()
    com.mathworks.widgets.text.mcode.analyzer.CodeAnalyzerUtils.applyAutoFixes(ed,msgs.get(i).getAutoFixChanges);
  end
end
Run Code Online (Sandbox Code Playgroud)

编辑: AHA!您可以获取mlint二进制文件以返回带有-fix标志的修复程序......这也适用于内置程序checkcode!仍然没有记录(据我所知),但可能比上述更强大:

>> checkcode(matlab.desktop.editor.getActiveFilename(),'-fix')
L 2 (C 3): Terminate statement with semicolon to suppress output (in functions).  (CAN FIX)
----FIX MESSAGE  <Add a semicolon.>
----CHANGE MESSAGE L 2 (C 13);  L 2 (C 12):   <;>
L 30 (C 52-53): Input argument 'in' might be unused. If this is OK, consider replacing it by ~.  (CAN FIX)
----FIX MESSAGE  <Replace name by ~.>
----CHANGE MESSAGE L 30 (C 52);  L 30 (C 53):   <~>
Run Code Online (Sandbox Code Playgroud)

在分配给一个结构时,这也揭示了@High Performance Mark在评论@gnovice答案时注意到的新fix领域的目的; 当有可用的修复时,它似乎是1,当消息是上面的时,它似乎是2,当消息是时,它似乎是4 .FIX MESSAGECHANGE MESSAGE

这是一个快速而又脏的Matlab函数,它返回一个给定m文件路径的"固定"字符串.没有错误检查等,它不会保存文件,因为我不保证它会工作.您还可以使用matlab.desktop.editorpublic(!)API来获取活动文档(getActive),并使用Text属性上的getter和setter 来修改文档而不保存它.

function str = applyAutoFixes(filepath)

msgs = checkcode(filepath,'-fix');

fid = fopen(filepath,'rt');
iiLine = 1;
lines = cell(0);
line = fgets(fid);
while ischar(line)
    lines{iiLine} = line;
    iiLine = iiLine+1;
    line = fgets(fid);
end
fclose(fid);

pos = [0 cumsum(cellfun('length',lines))];
str = [lines{:}];

fixes = msgs([msgs.fix] == 4);
%// Iterate backwards to try to prevent changing the indexing of 'str'
%// Note that two changes could still conflict with eachother. You could check
%// for this, or iteratively run mlint and fix one problem at a time.
for fix = fliplr(fixes(:)')
    %'// fix.column is a 2x2 - not sure what the second column is used for
    change_start = pos(fix.line(1)) + fix.column(1,1);
    change_end   = pos(fix.line(2)) + fix.column(2,1);

    if change_start >= change_end
        %// Seems to be an insertion
        str = [str(1:change_start) fix.message str(change_start+1:end)];
    else
        %// Seems to be a replacement
        str = [str(1:change_start-1) fix.message str(change_end+1:end)];
    end
end
Run Code Online (Sandbox Code Playgroud)

  • 我将Java appraoch添加到一个快捷方式,找到了一个漂亮的工具图标,它到目前为止运行顺畅.好的解决方案 (2认同)

小智 6

我知道这是一个老帖子,但我最近需要这个,并且对原始代码有所改进,所以如果有人需要它,那么它就是.它寻找缺少的";" 在函数中,不仅仅是在常规脚本中,在代码中维护空格并仅写入具有更改的文件.

function [] = add_semicolon(fileName)
%# Find the lines where a given mlint warning occurs:

mlintIDinScript = 'NOPTS';                       %# The ID of the warning
mlintIDinFunction = 'NOPRT';
mlintData = mlint(fileName,'-id');       %# Run mlint on the file
index = strcmp({mlintData.id},mlintIDinScript) | strcmp({mlintData.id},mlintIDinFunction);  %# Find occurrences of the warnings...
lineNumbers = [mlintData(index).line];   %#   ... and their line numbers

if isempty(lineNumbers)
    return;
end;
%# Read the lines of code from the file:

fid = fopen(fileName,'rt');
%linesOfCode = textscan(fid,'%s', 'Whitespace', '\n\r');  %# Read each line
lineNo = 0;
tline = fgetl(fid);
while ischar(tline)
    lineNo = lineNo + 1;
    linesOfCode{lineNo} = tline;
    tline = fgetl(fid);
end
fclose(fid);
%# Modify the lines of code:

%linesOfCode = linesOfCode{1};  %# Remove the outer cell array encapsulation
linesOfCode(lineNumbers) = strcat(linesOfCode(lineNumbers),';');  %# Add ';'

%# Write the lines of code back to the file:

fim = fopen(fileName,'wt');
fprintf(fim,'%s\n',linesOfCode{1:end-1});  %# Write all but the last line
fprintf(fim,'%s',linesOfCode{end});        %# Write the last line
fclose(fim);
Run Code Online (Sandbox Code Playgroud)