使用VIM修复格式错误的代码

时间:2011-08-25 01:42:44

标签: vim format

我需要'格式化/缩进'这段代码..你能建议修改吗?

import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class PrimeSearcher extends HttpServlet implements Runnable { long lastprime = 0; // last prime found Date lastprimeModified = new Date(); // when it was found Thread searcher; // background search thread public void init(ServletConfig config) throws ServletException { super.init(config); // always! searcher = new Thread(this); searcher.setPriority(Thread.MIN_PRIORITY); // be a good citizen searcher.start(); } public void run() { // QTTTBBBMMMTTTOOO long candidate = 1000000000000001L; // one quadrillion and one // Begin loop searching for primes while (true) { // search forever if (isPrime(candidate)) { lastprime = candidate; // new prime lastprimeModified = new Date(); // new "prime time" } candidate += 2; // evens aren't prime // Between candidates take a 0.2 second break. // Another way to be a good citizen with system resources. try { searcher.sleep(200); } catch (InterruptedException ignored) { } } } private static boolean isPrime(long candidate) { // Try dividing the number by all odd numbers between 3 and its sqrt double sqrt = Math.sqrt(candidate); for (long i = 3; i <= sqrt; i += 2) { if (candidate % i == 0) return false; // found a factor } // Wasn't evenly divisible, so it's prime return true; } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/plain"); PrintWriter out = res.getWriter(); if (lastprime == 0) { out.println("Still searching for first prime..."); } else { out.println("The last prime discovered was " + lastprime); out.println(" at " + lastprimeModified); } } public void destroy() { searcher.stop(); } }

4 个答案:

答案 0 :(得分:7)

围绕括号分隔线:

:%s/[{}]/\r&\r/g

分号后断行:

:%s/;/&\r/g

删除空行:

:g/^\s*$/d

缩进:

:set ft=java
gg=G

在此之后,您需要稍微调整代码以分割注释。


import java.io.;
import java.util.;
import javax.servlet.;
import javax.servlet.http.;
public class PrimeSearcher extends HttpServlet implements Runnable 
{
    long lastprime = 0;
    // last prime found Date lastprimeModified = new Date();
    // when it was found Thread searcher;
    // background search thread public void init(ServletConfig config) throws ServletException 
    {
        super.init(config);
        // always! searcher = new Thread(this);
        searcher.setPriority(Thread.MIN_PRIORITY);
        // be a good citizen searcher.start();
    }
    public void run() 
    {
        // QTTTBBBMMMTTTOOO long candidate = 1000000000000001L;
        // one quadrillion and one // Begin loop searching for primes while (true) 
        {
            // search forever if (isPrime(candidate)) 
            {
                lastprime = candidate;
                // new prime lastprimeModified = new Date();
                // new "prime time" 
            }
            candidate += 2;
            // evens aren't prime // Between candidates take a 0.2 second break. // Another way to be a good citizen with system resources. try 
            {
                searcher.sleep(200);
            }
            catch (InterruptedException ignored) 
            {
            }
        }
    }
    private static boolean isPrime(long candidate) 
    {
        // Try dividing the number by all odd numbers between 3 and its sqrt double sqrt = Math.sqrt(candidate);
        for (long i = 3;
                i <= sqrt;
                i += 2) 
        {
            if (candidate % i == 0) return false;
            // found a factor 
        }
        // Wasn't evenly divisible, so it's prime return true;
    }
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException 
    {
        res.setContentType("text/plain");
        PrintWriter out = res.getWriter();
        if (lastprime == 0) 
        {
            out.println("Still searching for first prime...");
        }
        else 
        {
            out.println("The last prime discovered was " + lastprime);
            out.println(" at " + lastprimeModified);
        }
    }
    public void destroy() 
    {
        searcher.stop();
    }
}

答案 1 :(得分:3)

您至少可以先用; + ;替换所有line-break

:%s/;/;\n\r/g

然后,您可以将所有{替换为{ + line-break

:%s/{/{\n\r/g

然后将所有}替换为} + line-break

:%s/}/}\n\r/g

这将帮助您入门。你仍然需要清理所有的缩进。太糟糕了,没有ReSharper for Java(我知道无论如何)。

答案 2 :(得分:2)

使用:

http://www.prettyprinter.de/

import java.io.;
import java.util.;
import javax.servlet.;
import javax.servlet.http.;
public class PrimeSearcher extends HttpServlet implements Runnable {
    long lastprime = 0;
    // last prime found Date lastprimeModified = new Date();
    // when it was found Thread searcher;
    // background search thread public void init(ServletConfig config) throws                 ServletException {
    super.init(config);
    // always! searcher = new Thread(this);
    searcher.setPriority(Thread.MIN_PRIORITY);
    // be a good citizen searcher.start();

}
public void run() {
    // QTTTBBBMMMTTTOOO long candidate = 1000000000000001L;
    // one quadrillion and one // Begin loop searching for primes while (true) {
        // search forever if (isPrime(candidate)) {
            lastprime = candidate;
            // new prime lastprimeModified = new Date();
            // new "prime time"
        }
        candidate += 2;
        // evens aren't prime // Between candidates take a 0.2 second break. // Another way to be a good citizen with system resources. try {
            searcher.sleep(200);

        }
        catch (InterruptedException ignored) {

        }

    }

}
private static boolean isPrime(long candidate) {
    // Try dividing the number by all odd numbers between 3 and its sqrt double sqrt = Math.sqrt(candidate);
    for (long i = 3;
    i &lt;
    = sqrt;
    i += 2) {
        if (candidate % i == 0) return false;
        // found a factor
    }
    // Wasn't evenly divisible, so it's prime return true;

}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    if (lastprime == 0) {
        out.println("Still searching for first prime...");

    }
    else {
        out.println("The last prime discovered was " + lastprime);
        out.println(" at " + lastprimeModified);

    }

}
public void destroy() {
    searcher.stop();

}

}

调整设置以使其显示您想要的效果。

答案 3 :(得分:0)

可能你最好按照以下步骤进行:

  • ;{}等字符后添加换行符:

    :s/[;{}]/\0\r/g
    
  • 浏览代码并修复坏的换行符。例如,以//开头的注释行通常不会以某些特殊字符结尾,并且需要手动拆分

  • 使用=修复缩进。将它与=G之类的移动命令一起使用,或者以可视模式选择代码,然后点击=缩进它。