下面列出了org.eclipse.jface.text.IDocument#getLegalLineDelimiters ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void updateModel(String prefix, String editablePart, String suffix) {
IDocument document = this.viewer.getDocument();
if (this.insertLineBreaks) {
String delimiter = document.getLegalLineDelimiters()[0];
if (document instanceof IDocumentExtension4) {
delimiter = ((IDocumentExtension4) document).getDefaultLineDelimiter();
}
prefix = prefix + delimiter;
suffix = delimiter + suffix;
}
String model = prefix + editablePart + suffix;
this.viewer.setRedraw(false);
this.viewer.getUndoManager().disconnect();
document.set(model);
this.viewer.setVisibleRegion(prefix.length(), editablePart.length());
this.viewer.getUndoManager().connect(this.viewer);
this.viewer.setRedraw(true);
}
public void updatePrefix(String prefix) {
try {
IDocument document = this.viewer.getDocument();
IRegion visibleRegion = this.viewer.getVisibleRegion();
String editablePart = document.get(visibleRegion.getOffset(), visibleRegion.getLength());
int suffixOffset = visibleRegion.getOffset() + visibleRegion.getLength();
String suffix = "";
if (document.getLength() - suffixOffset > 0) {
suffix = document.get(suffixOffset, document.getLength() - suffixOffset);
if (this.insertLineBreaks) {
String delimiter = document.getLegalLineDelimiters()[0];
if (document instanceof IDocumentExtension4) {
delimiter = ((IDocumentExtension4) document).getDefaultLineDelimiter();
}
suffix = suffix.substring(delimiter.length());
}
}
updateModel(prefix, editablePart, suffix);
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
}
@Override
protected void internalCustomizeDocumentCommand(IDocument document, DocumentCommand command)
throws BadLocationException {
if (command.length != 0)
return;
String[] lineDelimiters = document.getLegalLineDelimiters();
int delimiterIndex = TextUtilities.startsWith(lineDelimiters, command.text);
if (delimiterIndex != -1) {
MultiLineTerminalsEditStrategy bestStrategy = null;
IRegion bestStart = null;
for(MultiLineTerminalsEditStrategy strategy: strategies) {
IRegion candidate = strategy.findStartTerminal(document, command.offset);
if (candidate != null) {
if (bestStart == null || bestStart.getOffset() < candidate.getOffset()) {
bestStrategy = strategy;
bestStart = candidate;
}
}
}
if (bestStrategy != null) {
bestStrategy.internalCustomizeDocumentCommand(document, command);
}
}
}
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
if (!isSmartMode())
return;
if (command.text != null) {
if (command.length == 0) {
String[] lineDelimiters = document.getLegalLineDelimiters();
int index = TextUtilities.endsWith(lineDelimiters, command.text);
if (index > -1) {
// ends with line delimiter
if (lineDelimiters[index].equals(command.text))
// just the line delimiter
indentAfterNewLine(document, command);
return;
}
}
if (command.text.equals("/")) { //$NON-NLS-1$
indentAfterCommentEnd(document, command);
return;
}
}
}
@Override
protected void updatePatternViewer(Template template) {
if (template == null) {
getPatternViewer().getDocument().set(""); //$NON-NLS-1$
return ;
}
String contextId= template.getContextTypeId();
TemplateContextType type= getContextTypeRegistry().getContextType(contextId);
fTemplateProcessor.setContextType(type);
IDocument doc= getPatternViewer().getDocument();
String start= null;
if ("javadoc".equals(contextId)) { //$NON-NLS-1$
start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
} else
start= ""; //$NON-NLS-1$
doc.set(start + template.getPattern());
int startLen= start.length();
getPatternViewer().setDocument(doc, startLen, doc.getLength() - startLen);
}
@Override
public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
if (!isSmartMode())
return;
if (command.text != null) {
if (command.length == 0) {
String[] lineDelimiters= document.getLegalLineDelimiters();
int index= TextUtilities.endsWith(lineDelimiters, command.text);
if (index > -1) {
// ends with line delimiter
if (lineDelimiters[index].equals(command.text))
// just the line delimiter
indentAfterNewLine(document, command);
return;
}
}
if (command.text.equals("/")) { //$NON-NLS-1$
indentAfterCommentEnd(document, command);
return;
}
}
}
private boolean isLineDelimiter(IDocument document, DocumentCommand command) {
if (command.length != 0) {
return false;
}
String originalText = command.text;
String[] lineDelimiters = document.getLegalLineDelimiters();
int delimiterIndex = TextUtilities.startsWith(lineDelimiters, originalText);
return delimiterIndex != -1 && originalText.trim().length() == 0;
}
private boolean isLineDelimiter(IDocument document, DocumentCommand command) {
if (command.length != 0) {
return false;
}
String originalText = command.text;
String[] lineDelimiters = document.getLegalLineDelimiters();
int delimiterIndex = TextUtilities.startsWith(lineDelimiters, originalText);
return delimiterIndex != -1 && originalText.trim().length() == 0;
}
/**
* Returns a length of a line.
* @param document IDocument that contains the line.
* @param command DocumentCommand that determines the line.
* @param delim are line delimiters counted to the line length
* @param target -1 = previous line, 0 = current line, 1 = next line etc...
* @return the line length
*/
public int getLineLength(IDocument document, DocumentCommand command,
boolean delim, int target) {
int line;
int length = 0;
try{
line = document.getLineOfOffset(command.offset) + target;
if (line < 0 || line >= document.getNumberOfLines()){
//line = document.getLineOfOffset(command.offset);
return 0;
}
length = document.getLineLength(line);
if (length == 0){
return 0;
}
if (!delim){
String txt = document.get(document.getLineOffset(line), document.getLineLength(line));
String[] del = document.getLegalLineDelimiters();
int cnt = TextUtilities.endsWith(del ,txt);
if (!delim && cnt > -1){
length = length - del[cnt].length();
}
}
}catch(BadLocationException e){
TexlipsePlugin.log("TexEditorTools.getLineLength:",e);
}
return length;
}
/**
* Configures the scanner by providing access to the document range over which to scan.
*
* @param document the document to scan
* @param offset the offset of the document range to scan
* @param length the length of the document range to scan
*/
public final void setRange(IDocument document, int offset, int length) {
fDocument= document;
fRangeOffset= offset;
fRangeLength= length;
String[] delimiters= document.getLegalLineDelimiters();
fDelimiters= new char[delimiters.length][];
for (int i= 0; i < delimiters.length; i++)
fDelimiters[i]= delimiters[i].toCharArray();
updateBuffer(offset);
fOffset= 0;
}
/**
* True if text ends with a newline delimiter
*/
public static boolean endsWithNewline(IDocument document, String text) {
String[] newlines = document.getLegalLineDelimiters();
boolean ends = false;
for (int i = 0; i < newlines.length; i++) {
String delimiter = newlines[i];
if (text.indexOf(delimiter) != -1) {
ends = true;
}
}
return ends;
}
protected void updateViewerInput() {
IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection();
SourceViewer viewer= getViewer();
if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) {
TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement();
Template template= data.getTemplate();
String contextId= template.getContextTypeId();
TemplateContextType type= JSDTTypeScriptUIPlugin.getDefault().getTemplateContextRegistry().getContextType(contextId);
fTemplateProcessor.setContextType(type);
IDocument doc= viewer.getDocument();
String start= null;
if ("javadoc".equals(contextId)) { //$NON-NLS-1$
start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
} else
start= ""; //$NON-NLS-1$
doc.set(start + template.getPattern());
int startLen= start.length();
viewer.setDocument(doc, startLen, doc.getLength() - startLen);
} else {
viewer.getDocument().set(""); //$NON-NLS-1$
}
}
/**
* Configures the scanner by providing access to the document range over which to scan.
*
* @param document the document to scan
* @param offset the offset of the document range to scan
* @param length the length of the document range to scan
*/
public final void setRange(IDocument document, int offset, int length) {
fDocument= document;
fRangeOffset= offset;
fRangeLength= length;
String[] delimiters= document.getLegalLineDelimiters();
fDelimiters= new char[delimiters.length][];
for (int i= 0; i < delimiters.length; i++)
fDelimiters[i]= delimiters[i].toCharArray();
updateBuffer(offset);
fOffset= 0;
}
/**
* Configures the scanner by providing access to the document range over
* which to scan.
*
* @param document
* the document to scan
* @param offset
* the offset of the document range to scan
* @param length
* the length of the document range to scan
*/
public final void setRange(IDocument document, int offset, int length) {
fDocument = document;
fRangeOffset = offset;
fRangeLength = length;
String[] delimiters = document.getLegalLineDelimiters();
fDelimiters = new char[delimiters.length][];
for (int i = 0; i < delimiters.length; i++)
fDelimiters[i] = delimiters[i].toCharArray();
updateBuffer(offset);
fOffset = 0;
}
protected static boolean isLineDelimiter(IDocument d, String text)
{
String[] delimiters = d.getLegalLineDelimiters();
if (delimiters == null)
{
return false;
}
return TextUtilities.equals(delimiters, text) > -1;
}
@Override
public void setInformation(String content) {
super.setInformation(content);
IDocument doc= getViewer().getDocument();
if (doc == null)
return;
// ensure that we can scroll enough
ensureScrollable();
String start= null;
if (IJavaPartitions.JAVA_DOC.equals(fPartition)) {
start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
} else if (IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(fPartition)) {
start= "/*" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$
}
if (start != null) {
try {
doc.replace(0, 0, start);
int startLen= start.length();
getViewer().setDocument(doc, startLen, doc.getLength() - startLen);
} catch (BadLocationException e) {
// impossible
Assert.isTrue(false);
}
}
getViewer().getTextWidget().setHorizontalPixel(fHorizontalScrollPixel);
}
/**
* Performs indentation after new line is detected.
*
* @param document Document where new line is detected.
* @param command Command that represent the change of the document (new
* line).
*/
private void smartIndentAfterNewLine(IDocument document, DocumentCommand command) {
try {
itemSetted = false;
int commandOffset = command.offset;
int line = document.getLineOfOffset(commandOffset);
int lineOffset = document.getLineOffset(line);
String startLine = document.get(lineOffset, commandOffset - lineOffset);
//this is save
String lineDelimiter = document.getLegalLineDelimiters()
[TextUtilities.endsWith(document.getLegalLineDelimiters(), command.text)];
int beginIndex;
if ((beginIndex = LatexParserUtils.findCommand(startLine, "\\begin", 0)) != -1) {
// test if line contains \begin and search the environment (itemize,
// table...)
IRegion r = LatexParserUtils.getCommandArgument(startLine, beginIndex);
if (r == null){
//No environment found
super.customizeDocumentCommand(document, command);
return;
}
String envName = startLine.substring(r.getOffset(), r.getOffset()+r.getLength());
StringBuilder buf = new StringBuilder(command.text);
// get indentation of \begin
/* String prevIndentation = this.tools.getIndentation(document, line,
"\\begin", this.tabWidth); // NEW*/
String prevIndentation = getIndentation(startLine);
if (Arrays.binarySearch(this.indentationItems, envName) >= 0) {
buf.append(prevIndentation);
buf.append(this.indentationString);
} else {
buf.append(prevIndentation);
}
if (autoItem && (envName.equals("itemize") || envName.equals("enumerate"))) {
buf.append("\\item ");
itemSetted = true;
itemAtLine = document.getLineOfOffset(command.offset);
} else if (autoItem && envName.equals("description")) {
buf.append("\\item[]");
itemSetted = true;
itemAtLine = document.getLineOfOffset(command.offset);
}
command.caretOffset = command.offset + buf.length();
command.shiftsCaret = false;
if (autoItem && envName.equals("description")) {
command.caretOffset--;
}
/*
* looks for the \begin-statement and inserts
* an equivalent \end-statement (respects \begin-indentation)
*/
if (needsEnd(envName, document.get(), lineOffset)){
buf.append(lineDelimiter);
buf.append(prevIndentation);
buf.append("\\end{" + envName + "}");
}
command.text = buf.toString();
} else {
if (autoItem && !itemInserted(document, command)) {
super.customizeDocumentCommand(document, command);
} else {
super.customizeDocumentCommand(document, command);
}
}
} catch (BadLocationException e) {
TexlipsePlugin.log("TexAutoIndentStrategy:SmartIndentAfterNewLine", e);
}
}
private boolean isLineDelimiter(IDocument document, String text) {
String[] delimiters= document.getLegalLineDelimiters();
if (delimiters != null)
return TextUtilities.equals(delimiters, text) > -1;
return false;
}
private boolean isLineDelimiter(IDocument document, String text) {
String[] delimiters= document.getLegalLineDelimiters();
if (delimiters != null)
return TextUtilities.equals(delimiters, text) > -1;
return false;
}
private boolean isLineDelimiter(IDocument document, String text) {
String[] delimiters= document.getLegalLineDelimiters();
if (delimiters != null)
return TextUtilities.equals(delimiters, text) > -1;
return false;
}