mirror of
https://github.com/processing/processing4.git
synced 2026-02-12 18:10:43 +01:00
moving beautify into autoformat tool class
This commit is contained in:
115
app/Editor.java
115
app/Editor.java
@@ -584,8 +584,8 @@ public class Editor extends JFrame
|
||||
item = new JMenuItem("Auto Format");
|
||||
item.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
//new AutoFormat(Editor.this).show();
|
||||
handleBeautify();
|
||||
new AutoFormat(Editor.this).show();
|
||||
//handleBeautify();
|
||||
}
|
||||
});
|
||||
menu.add(item);
|
||||
@@ -1515,117 +1515,6 @@ public class Editor extends JFrame
|
||||
}
|
||||
|
||||
|
||||
// an improved algorithm that would still avoid a full state machine
|
||||
// 1. build an array of strings for the lines
|
||||
// 2. first remove everything between /* and */ (relentless)
|
||||
// 3. next remove anything inside two sets of " "
|
||||
// but not if escaped with a \
|
||||
// these can't extend beyond a line, so that works well
|
||||
// (this will save from "http://blahblah" showing up as a comment)
|
||||
// 4. remove from // to the end of a line everywhere
|
||||
// 5. run through remaining text to do indents
|
||||
// using hokey brace-counting algorithm
|
||||
// 6. also add indents for switch statements
|
||||
// case blah: { } (colons at end of line isn't a good way)
|
||||
// maybe /case \w+\:/
|
||||
public void handleBeautify() {
|
||||
String prog = textarea.getText();
|
||||
|
||||
// TODO re-enable history
|
||||
//history.record(prog, SketchHistory.BEAUTIFY);
|
||||
|
||||
int tabSize = Preferences.getInteger("editor.tabs.size");
|
||||
|
||||
char program[] = prog.toCharArray();
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
boolean gotBlankLine = false;
|
||||
int index = 0;
|
||||
int level = 0;
|
||||
|
||||
while (index != program.length) {
|
||||
int begin = index;
|
||||
while ((program[index] != '\n') &&
|
||||
(program[index] != '\r')) {
|
||||
index++;
|
||||
if (program.length == index)
|
||||
break;
|
||||
}
|
||||
int end = index;
|
||||
if (index != program.length) {
|
||||
if ((index+1 != program.length) &&
|
||||
// treat \r\n from windows as one line
|
||||
(program[index] == '\r') &&
|
||||
(program[index+1] == '\n')) {
|
||||
index += 2;
|
||||
} else {
|
||||
index++;
|
||||
}
|
||||
} // otherwise don't increment
|
||||
|
||||
String line = new String(program, begin, end-begin);
|
||||
line = line.trim();
|
||||
|
||||
if (line.length() == 0) {
|
||||
if (!gotBlankLine) {
|
||||
// let first blank line through
|
||||
buffer.append('\n');
|
||||
gotBlankLine = true;
|
||||
}
|
||||
} else {
|
||||
//System.out.println(level);
|
||||
int idx = -1;
|
||||
String myline = line.substring(0);
|
||||
while (myline.lastIndexOf('}') != idx) {
|
||||
idx = myline.indexOf('}');
|
||||
myline = myline.substring(idx+1);
|
||||
level--;
|
||||
}
|
||||
//for (int i = 0; i < level*2; i++) {
|
||||
// TODO i've since forgotten how i made this work (maybe it's even
|
||||
// a bug) but for now, level is incrementing/decrementing in
|
||||
// steps of two. in the interest of getting a release out,
|
||||
// i'm just gonna roll with that since this function will prolly
|
||||
// be replaced entirely and there are other things to worry about.
|
||||
for (int i = 0; i < tabSize * level / 2; i++) {
|
||||
buffer.append(' ');
|
||||
}
|
||||
buffer.append(line);
|
||||
buffer.append('\n');
|
||||
//if (line.charAt(0) == '{') {
|
||||
//level++;
|
||||
//}
|
||||
idx = -1;
|
||||
myline = line.substring(0);
|
||||
while (myline.lastIndexOf('{') != idx) {
|
||||
idx = myline.indexOf('{');
|
||||
myline = myline.substring(idx+1);
|
||||
level++;
|
||||
}
|
||||
gotBlankLine = false;
|
||||
}
|
||||
}
|
||||
|
||||
// save current (rough) selection point
|
||||
int selectionEnd = textarea.getSelectionEnd();
|
||||
|
||||
// replace with new bootiful text
|
||||
setText(buffer.toString(), false);
|
||||
|
||||
// make sure the caret would be past the end of the text
|
||||
if (buffer.length() < selectionEnd - 1) {
|
||||
selectionEnd = buffer.length() - 1;
|
||||
}
|
||||
|
||||
// at least in the neighborhood
|
||||
textarea.select(selectionEnd, selectionEnd);
|
||||
|
||||
//setSketchModified(true);
|
||||
//sketch.setCurrentModified(true);
|
||||
sketch.setModified();
|
||||
buttons.clear();
|
||||
}
|
||||
|
||||
|
||||
// TODO iron out bugs with this code under
|
||||
// different platforms, especially macintosh
|
||||
public void highlightLine(int lnum) {
|
||||
|
||||
160
app/tools/AutoFormat.java
Normal file
160
app/tools/AutoFormat.java
Normal file
@@ -0,0 +1,160 @@
|
||||
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||
|
||||
/*
|
||||
Part of the Processing project - http://processing.org
|
||||
|
||||
Copyright (c) 2004-05 Ben Fry and Casey Reas
|
||||
Copyright (c) 2001-04 Massachusetts Institute of Technology
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software Foundation,
|
||||
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
package processing.app.tools;
|
||||
|
||||
import processing.app.*;
|
||||
import processing.core.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.*;
|
||||
import javax.swing.event.*;
|
||||
|
||||
|
||||
/**
|
||||
* Basic but buggy handler for cleaning up code.
|
||||
* <PRE>
|
||||
* an improved algorithm that would still avoid a full state machine
|
||||
* 1. build an array of strings for the lines
|
||||
* 2. first remove everything between / * and * / (relentless)
|
||||
* 3. next remove anything inside two sets of " "
|
||||
* but not if escaped with a \
|
||||
* these can't extend beyond a line, so that works well
|
||||
* (this will save from "http://blahblah" showing up as a comment)
|
||||
* 4. remove from* to the end of a line everywhere
|
||||
* 5. run through remaining text to do indents
|
||||
* using hokey brace-counting algorithm
|
||||
* 6. also add indents for switch statements
|
||||
* case blah: { } (colons at end of line isn't a good way)
|
||||
* maybe /case \w+\:/
|
||||
* </PRE>
|
||||
*/
|
||||
public class AutoFormat {
|
||||
Editor editor;
|
||||
|
||||
|
||||
public AutoFormat(Editor editor) {
|
||||
this.editor = editor;
|
||||
}
|
||||
|
||||
|
||||
public void show() {
|
||||
String prog = editor.textarea.getText();
|
||||
|
||||
// TODO re-enable history
|
||||
//history.record(prog, SketchHistory.BEAUTIFY);
|
||||
|
||||
int tabSize = Preferences.getInteger("editor.tabs.size");
|
||||
|
||||
char program[] = prog.toCharArray();
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
boolean gotBlankLine = false;
|
||||
int index = 0;
|
||||
int level = 0;
|
||||
|
||||
while (index != program.length) {
|
||||
int begin = index;
|
||||
while ((program[index] != '\n') &&
|
||||
(program[index] != '\r')) {
|
||||
index++;
|
||||
if (program.length == index)
|
||||
break;
|
||||
}
|
||||
int end = index;
|
||||
if (index != program.length) {
|
||||
if ((index+1 != program.length) &&
|
||||
// treat \r\n from windows as one line
|
||||
(program[index] == '\r') &&
|
||||
(program[index+1] == '\n')) {
|
||||
index += 2;
|
||||
} else {
|
||||
index++;
|
||||
}
|
||||
} // otherwise don't increment
|
||||
|
||||
String line = new String(program, begin, end-begin);
|
||||
line = line.trim();
|
||||
|
||||
if (line.length() == 0) {
|
||||
if (!gotBlankLine) {
|
||||
// let first blank line through
|
||||
buffer.append('\n');
|
||||
gotBlankLine = true;
|
||||
}
|
||||
} else {
|
||||
//System.out.println(level);
|
||||
int idx = -1;
|
||||
String myline = line.substring(0);
|
||||
while (myline.lastIndexOf('}') != idx) {
|
||||
idx = myline.indexOf('}');
|
||||
myline = myline.substring(idx+1);
|
||||
level--;
|
||||
}
|
||||
//for (int i = 0; i < level*2; i++) {
|
||||
// TODO i've since forgotten how i made this work (maybe it's even
|
||||
// a bug) but for now, level is incrementing/decrementing in
|
||||
// steps of two. in the interest of getting a release out,
|
||||
// i'm just gonna roll with that since this function will prolly
|
||||
// be replaced entirely and there are other things to worry about.
|
||||
for (int i = 0; i < tabSize * level / 2; i++) {
|
||||
buffer.append(' ');
|
||||
}
|
||||
buffer.append(line);
|
||||
buffer.append('\n');
|
||||
//if (line.charAt(0) == '{') {
|
||||
//level++;
|
||||
//}
|
||||
idx = -1;
|
||||
myline = line.substring(0);
|
||||
while (myline.lastIndexOf('{') != idx) {
|
||||
idx = myline.indexOf('{');
|
||||
myline = myline.substring(idx+1);
|
||||
level++;
|
||||
}
|
||||
gotBlankLine = false;
|
||||
}
|
||||
}
|
||||
|
||||
// save current (rough) selection point
|
||||
int selectionEnd = editor.textarea.getSelectionEnd();
|
||||
|
||||
// replace with new bootiful text
|
||||
editor.setText(buffer.toString(), false);
|
||||
|
||||
// make sure the caret would be past the end of the text
|
||||
if (buffer.length() < selectionEnd - 1) {
|
||||
selectionEnd = buffer.length() - 1;
|
||||
}
|
||||
|
||||
// at least in the neighborhood
|
||||
editor.textarea.select(selectionEnd, selectionEnd);
|
||||
|
||||
editor.sketch.setModified();
|
||||
//buttons.clear();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
||||
|
||||
/*
|
||||
CreateFont - gui interface to font creation heaven/hell
|
||||
Part of the Processing project - http://processing.org
|
||||
|
||||
Copyright (c) 2004-05 Ben Fry and Casey Reas
|
||||
@@ -37,6 +36,9 @@ import javax.swing.border.*;
|
||||
import javax.swing.event.*;
|
||||
|
||||
|
||||
/**
|
||||
* gui interface to font creation heaven/hell.
|
||||
*/
|
||||
public class CreateFont extends JFrame {
|
||||
File targetFolder;
|
||||
|
||||
|
||||
1
todo.txt
1
todo.txt
@@ -549,6 +549,7 @@ _ http://processing.org/discourse/yabb/YaBB.cgi?board=Proce55ing_Software;ac
|
||||
|
||||
TOOLS / Create Font
|
||||
|
||||
1 _ create the tool object on startup, then use thread to getAllFonts()
|
||||
1 _ close/hide window on 'ESC'
|
||||
1 _ loading is very slow on the first time (getting all font names)
|
||||
1 _ show a progress/status bar while it's happening?
|
||||
|
||||
Reference in New Issue
Block a user