Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using VIM to fix badly formatted code

Tags:

vim

format

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 // 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(); } }
like image 766
shergill Avatar asked Jul 10 '26 09:07

shergill


2 Answers

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();
    }
}
like image 143
Don Reba Avatar answered Jul 13 '26 17:07

Don Reba


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).

like image 38
Jason Down Avatar answered Jul 13 '26 17:07

Jason Down



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!