use of processing.app.SketchCode in project processing by processing.
the class JavaTextAreaPainter method updateTweakInterfacePositions.
/**
* Initialize all the number changing interfaces.
* synchronize this method to prevent the execution of 'paint' in the middle.
* (don't paint while we make changes to the text of the editor)
*/
private synchronized void updateTweakInterfacePositions() {
SketchCode[] code = getEditor().getSketch().getCode();
int prevScroll = textArea.getVerticalScrollPosition();
String prevText = textArea.getText();
for (int tab = 0; tab < code.length; tab++) {
String tabCode = getJavaEditor().baseCode[tab];
textArea.setText(tabCode);
for (Handle n : handles.get(tab)) {
int lineStartChar = textArea.getLineStartOffset(n.line);
int x = textArea.offsetToX(n.line, n.newStartChar - lineStartChar);
int end = textArea.offsetToX(n.line, n.newEndChar - lineStartChar);
int y = textArea.lineToY(n.line) + fm.getHeight() + 1;
n.initInterface(x, y, end - x, fm.getHeight());
}
for (ColorControlBox cBox : colorBoxes.get(tab)) {
int lineStartChar = textArea.getLineStartOffset(cBox.getLine());
int x = textArea.offsetToX(cBox.getLine(), cBox.getCharIndex() - lineStartChar);
int y = textArea.lineToY(cBox.getLine()) + fm.getDescent();
cBox.initInterface(this, x, y + 1, fm.getHeight() - 2, fm.getHeight() - 2);
}
}
textArea.setText(prevText);
textArea.scrollTo(prevScroll, 0);
}
use of processing.app.SketchCode in project processing by processing.
the class JavaTextAreaPainter method updateCodeText.
/**
* Take the saved code of the current tab and replace
* all numbers with their current values.
* Update TextArea with the new code.
*/
public void updateCodeText() {
int charInc = 0;
int currentTab = getCurrentCodeIndex();
SketchCode sc = getEditor().getSketch().getCode(currentTab);
String code = getJavaEditor().baseCode[currentTab];
for (Handle n : handles.get(currentTab)) {
int s = n.startChar + charInc;
int e = n.endChar + charInc;
code = replaceString(code, s, e, n.strNewValue);
n.newStartChar = n.startChar + charInc;
charInc += n.strNewValue.length() - n.strValue.length();
n.newEndChar = n.endChar + charInc;
}
replaceTextAreaCode(code);
// update also the sketch code for later
sc.setProgram(code);
}
use of processing.app.SketchCode in project processing by processing.
the class PreprocessingService method preprocessSketch.
/// --------------------------------------------------------------------------
private PreprocessedSketch preprocessSketch(PreprocessedSketch prevResult) {
boolean firstCheck = prevResult == null;
PreprocessedSketch.Builder result = new PreprocessedSketch.Builder();
List<ImportStatement> codeFolderImports = result.codeFolderImports;
List<ImportStatement> programImports = result.programImports;
JavaMode javaMode = (JavaMode) editor.getMode();
Sketch sketch = result.sketch = editor.getSketch();
String className = sketch.getName();
StringBuilder workBuffer = new StringBuilder();
// Combine code into one buffer
IntList tabStartsList = new IntList();
for (SketchCode sc : sketch.getCode()) {
if (sc.isExtension("pde")) {
tabStartsList.append(workBuffer.length());
try {
workBuffer.append(sc.getDocumentText());
} catch (BadLocationException e) {
e.printStackTrace();
}
workBuffer.append('\n');
}
}
result.tabStartOffsets = tabStartsList.array();
String pdeStage = result.pdeCode = workBuffer.toString();
boolean reloadCodeFolder = firstCheck || codeFolderChanged.getAndSet(false);
boolean reloadLibraries = firstCheck || librariesChanged.getAndSet(false);
// Core and default imports
if (coreAndDefaultImports == null) {
PdePreprocessor p = editor.createPreprocessor(null);
coreAndDefaultImports = buildCoreAndDefaultImports(p);
}
result.coreAndDefaultImports.addAll(coreAndDefaultImports);
// Prepare code folder imports
if (reloadCodeFolder) {
codeFolderImports.addAll(buildCodeFolderImports(sketch));
} else {
codeFolderImports.addAll(prevResult.codeFolderImports);
}
// TODO: convert unicode escapes to chars
SourceUtils.scrubCommentsAndStrings(workBuffer);
Mode sketchMode = PdePreprocessor.parseMode(workBuffer);
// Prepare transforms to convert pde code into parsable code
TextTransform toParsable = new TextTransform(pdeStage);
toParsable.addAll(SourceUtils.insertImports(coreAndDefaultImports));
toParsable.addAll(SourceUtils.insertImports(codeFolderImports));
toParsable.addAll(SourceUtils.parseProgramImports(workBuffer, programImports));
toParsable.addAll(SourceUtils.replaceTypeConstructors(workBuffer));
toParsable.addAll(SourceUtils.replaceHexLiterals(workBuffer));
toParsable.addAll(SourceUtils.wrapSketch(sketchMode, className, workBuffer.length()));
{
// Refresh sketch classloader and classpath if imports changed
if (javaRuntimeClassPath == null) {
javaRuntimeClassPath = buildJavaRuntimeClassPath();
sketchModeClassPath = buildModeClassPath(javaMode, false);
searchModeClassPath = buildModeClassPath(javaMode, true);
}
if (reloadLibraries) {
coreLibraryClassPath = buildCoreLibraryClassPath(javaMode);
}
boolean rebuildLibraryClassPath = reloadLibraries || checkIfImportsChanged(programImports, prevResult.programImports);
if (rebuildLibraryClassPath) {
sketchLibraryClassPath = buildSketchLibraryClassPath(javaMode, programImports);
searchLibraryClassPath = buildSearchLibraryClassPath(javaMode);
}
boolean rebuildClassPath = reloadCodeFolder || rebuildLibraryClassPath || prevResult.classLoader == null || prevResult.classPath == null || prevResult.classPathArray == null || prevResult.searchClassPathArray == null;
if (reloadCodeFolder) {
codeFolderClassPath = buildCodeFolderClassPath(sketch);
}
if (rebuildClassPath) {
{
// Sketch class path
List<String> sketchClassPath = new ArrayList<>();
sketchClassPath.addAll(javaRuntimeClassPath);
sketchClassPath.addAll(sketchModeClassPath);
sketchClassPath.addAll(sketchLibraryClassPath);
sketchClassPath.addAll(coreLibraryClassPath);
sketchClassPath.addAll(codeFolderClassPath);
String[] classPathArray = sketchClassPath.stream().toArray(String[]::new);
URL[] urlArray = Arrays.stream(classPathArray).map(path -> {
try {
return Paths.get(path).toUri().toURL();
} catch (MalformedURLException e) {
Messages.loge("malformed URL when preparing sketch classloader", e);
return null;
}
}).filter(url -> url != null).toArray(URL[]::new);
result.classLoader = new URLClassLoader(urlArray, null);
result.classPath = classPathFactory.createFromPaths(classPathArray);
result.classPathArray = classPathArray;
}
{
// Search class path
List<String> searchClassPath = new ArrayList<>();
searchClassPath.addAll(javaRuntimeClassPath);
searchClassPath.addAll(searchModeClassPath);
searchClassPath.addAll(searchLibraryClassPath);
searchClassPath.addAll(coreLibraryClassPath);
searchClassPath.addAll(codeFolderClassPath);
result.searchClassPathArray = searchClassPath.stream().toArray(String[]::new);
}
} else {
result.classLoader = prevResult.classLoader;
result.classPath = prevResult.classPath;
result.searchClassPathArray = prevResult.searchClassPathArray;
result.classPathArray = prevResult.classPathArray;
}
}
{
// Check for missing braces
List<JavaProblem> missingBraceProblems = SourceUtils.checkForMissingBraces(workBuffer, result.tabStartOffsets);
if (!missingBraceProblems.isEmpty()) {
result.missingBraceProblems.addAll(missingBraceProblems);
result.hasSyntaxErrors = true;
}
}
// Transform code to parsable state
String parsableStage = toParsable.apply();
OffsetMapper parsableMapper = toParsable.getMapper();
// Create intermediate AST for advanced preprocessing
CompilationUnit parsableCU = makeAST(parser, parsableStage.toCharArray(), COMPILER_OPTIONS);
// Prepare advanced transforms which operate on AST
TextTransform toCompilable = new TextTransform(parsableStage);
toCompilable.addAll(SourceUtils.preprocessAST(parsableCU));
// Transform code to compilable state
String compilableStage = toCompilable.apply();
OffsetMapper compilableMapper = toCompilable.getMapper();
char[] compilableStageChars = compilableStage.toCharArray();
// Create compilable AST to get syntax problems
CompilationUnit compilableCU = makeAST(parser, compilableStageChars, COMPILER_OPTIONS);
// Get syntax problems from compilable AST
result.hasSyntaxErrors |= Arrays.stream(compilableCU.getProblems()).anyMatch(IProblem::isError);
// Generate bindings after getting problems - avoids
// 'missing type' errors when there are syntax problems
CompilationUnit bindingsCU = makeASTWithBindings(parser, compilableStageChars, COMPILER_OPTIONS, className, result.classPathArray);
// Get compilation problems
List<IProblem> bindingsProblems = Arrays.asList(bindingsCU.getProblems());
result.hasCompilationErrors = bindingsProblems.stream().anyMatch(IProblem::isError);
// Update builder
result.offsetMapper = parsableMapper.thenMapping(compilableMapper);
result.javaCode = compilableStage;
result.compilationUnit = bindingsCU;
// Build it
return result.build();
}
Aggregations