Skip to content

Commit

Permalink
Fixed tab characters conversion from loaded files and pasted text
Browse files Browse the repository at this point in the history
  • Loading branch information
maccasoft committed Mar 26, 2024
1 parent e93da53 commit 96f6730
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

package com.maccasoft.propeller.spin1;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.PrintStream;

Expand All @@ -23,6 +21,7 @@

import com.maccasoft.propeller.Compiler.FileSourceProvider;
import com.maccasoft.propeller.CompilerException;
import com.maccasoft.propeller.internal.FileUtils;
import com.maccasoft.propeller.model.Node;

class Spin1ExamplesTest {
Expand Down Expand Up @@ -340,7 +339,7 @@ void compileAndCompare(File source) throws Exception {
}

void compileAndCompare(File source, File binary) throws Exception {
String text = loadFromFile(source);
String text = FileUtils.loadFromFile(source);
byte[] expected = loadBinaryFromFile(binary);

Spin1TokenStream stream = new Spin1TokenStream(text);
Expand Down Expand Up @@ -374,24 +373,6 @@ void compileAndCompare(File source, File binary) throws Exception {
Assertions.assertEquals(expectedListing, actualListing);
}

String loadFromFile(File file) {
String line;
StringBuilder sb = new StringBuilder();

try {
BufferedReader reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
reader.close();
} catch (Exception e) {
throw new RuntimeException("error reading file " + file, e);
}

return sb.toString();
}

byte[] loadBinaryFromFile(File file) {
try {
InputStream is = new FileInputStream(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

package com.maccasoft.propeller.spin1;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.PrintStream;

Expand All @@ -23,6 +21,7 @@

import com.maccasoft.propeller.Compiler.FileSourceProvider;
import com.maccasoft.propeller.CompilerException;
import com.maccasoft.propeller.internal.FileUtils;
import com.maccasoft.propeller.model.Node;

class Spin1LibraryTest {
Expand Down Expand Up @@ -342,7 +341,7 @@ void test_jm_time_80() throws Exception {
}

void compileAndCompare(File source, File binary) throws Exception {
String text = loadFromFile(source);
String text = FileUtils.loadFromFile(source);
byte[] expected = loadBinaryFromFile(binary);

Spin1TokenStream stream = new Spin1TokenStream(text);
Expand Down Expand Up @@ -375,26 +374,6 @@ void compileAndCompare(File source, File binary) throws Exception {
Assertions.assertEquals(expectedListing, actualListing);
}

String loadFromFile(File file) {
String line;
StringBuilder sb = new StringBuilder();

if (file.exists()) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
reader.close();
} catch (Exception e) {
throw new RuntimeException("error reading file " + file, e);
}
}

return sb.toString();
}

byte[] loadBinaryFromFile(File file) throws Exception {
InputStream is = new FileInputStream(file);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.maccasoft.propeller.Compiler.FileSourceProvider;
import com.maccasoft.propeller.CompilerException;
import com.maccasoft.propeller.internal.FileUtils;
import com.maccasoft.propeller.model.Node;

class Spin2ExamplesTest {
Expand Down Expand Up @@ -186,7 +187,7 @@ void test_flash_loader() throws Exception {
}

void compileAndCompare(File source, File binary) throws Exception {
String text = loadFromFile(source);
String text = FileUtils.replaceTabs(loadFromFile(source), 8);
byte[] expected = loadBinaryFromFile(binary);

Spin2TokenStream stream = new Spin2TokenStream(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

package com.maccasoft.propeller.spin2;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.PrintStream;

Expand All @@ -23,6 +21,7 @@

import com.maccasoft.propeller.Compiler.FileSourceProvider;
import com.maccasoft.propeller.CompilerException;
import com.maccasoft.propeller.internal.FileUtils;
import com.maccasoft.propeller.model.Node;

class Spin2LibraryTest {
Expand Down Expand Up @@ -272,7 +271,7 @@ void test_vga_tile_driver() throws Exception {
}

void compileAndCompare(File source, File binary) throws Exception {
String text = loadFromFile(source);
String text = FileUtils.loadFromFile(source);
byte[] expected = loadBinaryFromFile(binary);

Spin2TokenStream stream = new Spin2TokenStream(text);
Expand Down Expand Up @@ -323,24 +322,6 @@ void compileAndCompare(File source, File binary) throws Exception {
Assertions.assertEquals(expectedListing, actualListing);
}

String loadFromFile(File file) {
String line;
StringBuilder sb = new StringBuilder();

try {
BufferedReader reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
reader.close();
} catch (Exception e) {
throw new RuntimeException("error reading file " + file, e);
}

return sb.toString();
}

byte[] loadBinaryFromFile(File file) {
try {
InputStream is = new FileInputStream(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import com.maccasoft.propeller.SourceTokenMarker.TokenMarker;
import com.maccasoft.propeller.internal.ColorRegistry;
import com.maccasoft.propeller.internal.ContentProposalAdapter;
import com.maccasoft.propeller.internal.FileUtils;
import com.maccasoft.propeller.internal.HTMLStyledTextDecorator;
import com.maccasoft.propeller.internal.IContentProposalListener2;
import com.maccasoft.propeller.internal.StyledTextContentAdapter;
Expand Down Expand Up @@ -342,6 +343,10 @@ public void modifyText(ModifyEvent e) {

@Override
public void verifyText(VerifyEvent e) {
if (e.text.indexOf('\t') != -1) {
e.text = FileUtils.replaceTabs(e.text, styledText.getTabs());
}

String replacedText = styledText.getTextRange(e.start, e.end - e.start);
if (!ignoreUndo) {
if (currentChange == null || currentChange.isExpired()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,29 @@ public static String loadFromFile(File file) throws IOException {
data = s.getBytes("UTF-8");
}

return new String(data).replaceAll(EOL_PATTERN, System.lineSeparator());
String text = new String(data).replaceAll(EOL_PATTERN, System.lineSeparator());

return replaceTabs(text, 8);
}

public static String replaceTabs(String text, int tabs) {
String spaces = " ".repeat(tabs);

int i = 0;
while ((i = text.indexOf('\t', i)) != -1) {
int s = i;
while (s > 0) {
s--;
if (text.charAt(s) == '\r' || text.charAt(s) == '\n') {
s++;
break;
}
}
int n = ((i - s) % tabs);
text = text.substring(0, i) + spaces.substring(0, tabs - n) + text.substring(i + 1);
}

return text;
}

public static byte[] loadBinaryFromFile(File file) throws Exception {
Expand Down

0 comments on commit 96f6730

Please sign in to comment.