-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,113 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
RSyntaxTextArea/src/main/java/org/fife/ui/rtextarea/RContent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.fife.ui.rtextarea; | ||
|
||
import javax.swing.text.BadLocationException; | ||
|
||
public interface RContent { | ||
char charAt(int offset) throws BadLocationException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
RSyntaxTextArea/src/main/java/org/fife/ui/rtextarea/RGapContent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.fife.ui.rtextarea; | ||
|
||
import javax.swing.text.BadLocationException; | ||
import javax.swing.text.GapContent; | ||
|
||
/** | ||
* Document content that provides fast access to individual characters. | ||
*/ | ||
public class RGapContent extends GapContent implements RContent { | ||
|
||
@Override | ||
public char charAt(int offset) throws BadLocationException { | ||
if( offset < 0 || offset >= length() ){ | ||
throw new BadLocationException("Invalid offset", offset); | ||
} | ||
int g0 = getGapStart(); | ||
char[] array = (char[]) getArray(); | ||
if( offset < g0 ){ // below gap | ||
return array[offset]; | ||
} | ||
return array[getGapEnd() + offset - g0]; // above gap | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
RSyntaxTextArea/src/main/java/org/fife/ui/rtextarea/readonly/FileOffsetLineDiscover.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.fife.ui.rtextarea.readonly; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.*; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
class FileOffsetLineDiscover { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(FileOffsetLineDiscover.class.getName()); | ||
|
||
private FileOffsetLineDiscover() { | ||
} | ||
|
||
public static List<Integer> getOffsets(Path filePath, Charset charset) { | ||
|
||
List<Integer> lfOffsets = new ArrayList<>(); | ||
Set<Integer> crOffsets = new HashSet<>(); | ||
|
||
List<Integer> lineOffsets = new ArrayList<>(); | ||
|
||
lineOffsets.add(0); | ||
|
||
readFile(filePath, charset, lfOffsets, crOffsets); | ||
|
||
for( Integer newLineIndex : lfOffsets ){ | ||
|
||
//reconize \n\r LFCR | ||
if( crOffsets.contains(newLineIndex + 1) ){ | ||
int lfcrIndex = newLineIndex + 1; | ||
lineOffsets.add(lfcrIndex); | ||
crOffsets.remove(lfcrIndex); | ||
} | ||
|
||
//recognize \r\n CRLF | ||
else if( crOffsets.contains(newLineIndex - 1) ){ | ||
int crlfOffset = newLineIndex - 1; | ||
lineOffsets.add(newLineIndex); | ||
crOffsets.remove(crlfOffset); | ||
} | ||
|
||
//add LF | ||
else lineOffsets.add(newLineIndex); | ||
} | ||
|
||
lineOffsets.addAll(crOffsets); | ||
lineOffsets.sort(Integer::compare); | ||
return lineOffsets; | ||
} | ||
|
||
|
||
private static void readFile(Path filePath, Charset fixedSizeCharset, Collection<Integer> newLines, Collection<Integer> carrigeRetrun) { | ||
try( BufferedReader bufferedReader = Files.newBufferedReader(filePath, fixedSizeCharset); ){ | ||
int offset = 0; | ||
char character; | ||
int readValue; | ||
while( true ){ | ||
readValue = bufferedReader.read(); | ||
if( readValue == -1 ) return; | ||
offset++; | ||
character = (char) readValue; | ||
if( character == '\n' ){ | ||
newLines.add(offset); | ||
} else if( character == '\r' ){ | ||
carrigeRetrun.add(offset); | ||
} | ||
|
||
|
||
} | ||
} catch( IOException e ){ | ||
LOGGER.log(Level.WARNING, "Unable to read line offsets from file", e); | ||
} | ||
} | ||
} |
Oops, something went wrong.