Here you can find the source of formatPython(int start, Document doc, String s)
private static String formatPython(int start, Document doc, String s) throws BadLocationException
//package com.java2s; //License from project: Open Source License import java.util.NoSuchElementException; import java.util.Scanner; import javax.swing.text.BadLocationException; import javax.swing.text.Document; public class Main { private static String formatPython(int start, Document doc, String s) throws BadLocationException { int pos = 0; int prev; boolean check = false; String spaces = ""; String formattedLine = ""; String scannedLine = ""; Scanner src = new Scanner(doc.getText(0, doc.getLength())); try {//from w w w .j a v a 2 s . c om while (!check) { String nextLine = src.nextLine(); prev = pos; pos += nextLine.length() + 1; if (pos > start) { check = true; pos -= prev; } } } catch (NoSuchElementException e) { return s; } for (int j = 0; j < pos - 1; j++) { spaces += " "; } Scanner lineCheck = new Scanner(s); int count = 0; while (lineCheck.hasNext()) { scannedLine = lineCheck.nextLine(); if (count == 0) { formattedLine += scannedLine; count++; } else { formattedLine += '\n' + spaces + scannedLine; } } return formattedLine; } }