开发者

Using VIM to fix badly formatted code

Ihave this piece of code that i need to 'format/indent'.. Can you please suggest a fix?

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 // Begi开发者_JS百科n 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(); } }


Break lines around brackets:

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

Break lines after semicolons:

:%s/;/&\r/g

Remove empty lines:

:g/^\s*$/d

Indent:

:set ft=java
gg=G

After this, you need to tweak the code just a little to split comments.


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();
    }
}


You could at least start by replacing all ; with a ; + line-break:

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

Then you could replace all { with { + line-break:

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

Then replace all } with } + line-break:

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

This will get you started. You'd still have to clean up all the indentation. Too bad there isn't ReSharper for Java (that I know of anyway).


Use:

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();

}

}

Tweak the settings to get it looking how you want.


Probably you best proceed as following:

  • Add line breaks after characters like ;, { and }:

    :s/[;{}]/\0\r/g
    
  • Go through the code and fix bad line breaks. For example comment lines started with // don't usually end in some special character and will need manual splitting

  • Fix the indentation with =. Use it with some movement command like =G or select the code in visual mode and then hit = to indent it.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜