下面列出了org.eclipse.jface.text.IDocument#get ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns the indentation string (tabs and spaces) from the insertion point line.
*
* @return the indentation string
*/
private String getIndentation() {
StringBuilder res = new StringBuilder();
IDocument document= getDocument();
try {
IRegion region= document.getLineInformationOfOffset(getStart());
String lineContent= document.get(region.getOffset(), region.getLength());
for (int i=0; i<lineContent.length(); ++i) {
char ch = lineContent.charAt(i);
if (ch == ' ' || ch == '\t') {
res.append(ch);
} else {
break;
}
}
} catch (BadLocationException e) {
LogHelper.logError(e);
}
return res.toString();
}
@Override
public void customizeDocumentCommand(final IDocument document, final DocumentCommand command) {
try {
if ((command.text.equals("t") && document.get((command.offset - 4), 4).equals("Xtex"))) {
this.contents = document.get();
document.set(this.getText());
command.text = "";
int _lastIndexOf = this.getText().lastIndexOf("M");
int _plus = (_lastIndexOf + 1);
command.offset = _plus;
} else {
if ((this.contents != null)) {
document.set(this.contents);
this.contents = null;
}
}
} catch (final Throwable _t) {
if (_t instanceof BadLocationException) {
} else {
throw Exceptions.sneakyThrow(_t);
}
}
}
public static String nl_to_cursor( IDocument doc, int offset){
try {
for (int n = offset - 1; n >= 0; n--) {
char c = doc.getChar(n);
if ( !BaseUtils.isHttp4eIdentifier(c) || c == AssistConstants.EQUAL) {
String res1 = doc.get(n + 1, offset - n - 1);
return res1;
} else if (n == 0) {
String res2 = doc.get(n, offset);
return res2;
}
}
} catch (BadLocationException ignore) {
ExceptionHandler.warn("lastWord: " + ignore);
}
return "";
}
protected AbstractDocumentScanner(IDocument document, String partitioning, String contentType) {
this.document = assertNotNull(document);
this.partitioning = partitioning;
this.source = document.get();
if(partitioning == null) {
this.contentType = IDocument.DEFAULT_CONTENT_TYPE;
this.documentExt3 = null;
this.partitioner = null;
} else {
Assert.isLegal(partitioning != null);
Assert.isLegal(contentType != null);
this.contentType = contentType;
this.documentExt3 = tryCast(document, IDocumentExtension3.class);
Assert.isLegal(documentExt3 != null, "document must support IDocumentExtension3");
this.partitioner = documentExt3.getDocumentPartitioner(partitioning);
Assert.isLegal(partitioner != null, "document must have a partitioner for " + partitioning);
}
}
public boolean validate(IDocument document, int offset, DocumentEvent event)
{
try
{
int replaceOffset = getReplaceOffset(document, fTemplate);
if (offset >= replaceOffset)
{
String content = document.get(replaceOffset, offset - replaceOffset);
return fTemplate.getName().startsWith(content);
}
}
catch (BadLocationException e)
{
// concurrent modification - ignore
}
return false;
}
@Override
public String getKey() {
if (getCompletionLength() == 0)
return super.getKey();
try {
IDocument document= getDocument();
int start= getStart();
int end= getCompletionOffset();
return start <= end
? document.get(start, end - start)
: ""; //$NON-NLS-1$
} catch (BadLocationException e) {
return super.getKey();
}
}
public String getLineIndentation(final IDocument document, final int offset) throws BadLocationException {
String _xblockexpression = null;
{
int _xifexpression = (int) 0;
int _length = document.getLength();
boolean _tripleEquals = (offset == _length);
if (_tripleEquals) {
_xifexpression = (offset - 1);
} else {
_xifexpression = offset;
}
int adjustedOffset = _xifexpression;
IRegion line = document.getLineInformationOfOffset(adjustedOffset);
int start = line.getOffset();
int end = this.findEndOfWhiteSpace(document, start, offset);
_xblockexpression = document.get(start, (end - start));
}
return _xblockexpression;
}
public boolean validate(IDocument document, int offset, DocumentEvent event) {
try {
String content= document.get(fReplacementPosition.getOffset(), offset - fReplacementPosition.getOffset());
if (fReplacementString.startsWith(content))
return true;
} catch (BadLocationException e) {
// ignore concurrently modified document
}
return false;
}
/**
* Checks a partition of a document for todo tags (filling lst with the markers to be created).
*/
private void check(int i, int j, IDocument document, List<MarkerInfo> lst, List<String> todoTags)
throws BadLocationException {
String tok = document.get(i, j - i);
int index;
HashSet<Integer> lines = new HashSet<Integer>();
for (String element : todoTags) {
if (element.length() == 0) {
continue;
}
int start = 0;
while ((index = tok.indexOf(element, start)) != -1) {
start = index + element.length();
int absoluteStart = i + index;
int line = document.getLineOfOffset(absoluteStart);
if (lines.contains(line)) {
//Only 1 TASK per line!
continue;
} else {
lines.add(line);
}
String message = tok.substring(index).trim();
String markerType = IMarker.TASK;
int severity = IMarker.SEVERITY_WARNING;
boolean userEditable = false;
boolean isTransient = false;
int absoluteEnd = absoluteStart + message.length();
Map<String, Object> additionalInfo = null;
MarkerInfo markerInfo = new PyMarkerUtils.MarkerInfo(document, message, markerType, severity,
userEditable, isTransient, line, absoluteStart, absoluteEnd, additionalInfo);
lst.add(markerInfo);
}
}
}
/**
* Returns the line number (in Java coordinates/zero based) of the line containing the first
* "--algorithm" or "--fair algorithm" token(s) that begin(s) a PlusCal algorithm.
* Returns -1 if there is none.
*
* @param document
* @return
*/
public static int GetLineOfPCalAlgorithm(IDocument document) {
try {
final String moduleAsString = document.get();
return LocationToLine(document,
TLAtoPCalMapping.GetLineOfPCalAlgorithm(moduleAsString));
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
/**
* Returns whether the word is "inheritDoc".
*
* @param document the document
* @param wordRegion the word region
* @return <code>true</code> iff the word is "inheritDoc"
* @since 3.7
*/
private static boolean isInheritDoc(IDocument document, IRegion wordRegion) {
try {
String word= document.get(wordRegion.getOffset(), wordRegion.getLength());
return "inheritDoc".equals(word); //$NON-NLS-1$
} catch (BadLocationException e) {
return false;
}
}
/**
* Expects the cursor to be in the same line as the start terminal puts any text between start terminal and cursor
* into a separate newline before the cursor. puts any text between cursor and end terminal into a separate newline
* after the cursor. puts the closing terminal into a separate line at the end. adds a closing terminal if not
* existent. If the next astElement is a method with parameters or return the JSDoc-tags will be added as an
* addition.
*/
@Override
protected CommandInfo handleCursorInFirstLine(IDocument document, DocumentCommand command, IRegion startTerminal,
IRegion stopTerminal) throws BadLocationException {
CommandInfo newC = new CommandInfo();
List<String> returnTypeAndParameterNames = getReturnTypeAndParameterNames(document, startTerminal);
String paramString = "";
String returnString = "";
if ((returnTypeAndParameterNames.size() > 0) && returnTypeAndParameterNames.get(0).equals("return")) {
returnString = INDENTATION_STR + RETURN_STR + command.text;
}
if (returnTypeAndParameterNames.size() > 1) {
for (int i = 1; i < returnTypeAndParameterNames.size(); i += 1) {
paramString += command.text + INDENTATION_STR + PARAM_STR + returnTypeAndParameterNames.get(i);
}
}
newC.isChange = true;
newC.offset = command.offset;
newC.text += command.text + INDENTATION_STR;
newC.cursorOffset = command.offset + newC.text.length();
if (stopTerminal == null && atEndOfLineInput(document, command.offset)) {
newC.text += command.text + getRightTerminal();
}
if (stopTerminal != null && stopTerminal.getOffset() >= command.offset
&& util.isSameLine(document, stopTerminal.getOffset(), command.offset)) {
String string = document.get(command.offset, stopTerminal.getOffset() - command.offset);
if (string.trim().length() > 0)
newC.text += string.trim();
if (!(returnTypeAndParameterNames.size() == 0)) {
newC.text += paramString + command.text + returnString;
} else {
newC.text += command.text;
}
newC.length += string.length();
}
return newC;
}
@Override
public boolean validate(IDocument document, int offset, DocumentEvent event) {
if (getContext() instanceof JavaStatementPostfixContext) {
JavaStatementPostfixContext c = (JavaStatementPostfixContext) getContext();
try {
int start = c.getStart() + c.getAffectedSourceRegion().getLength() + 1;
String content = document.get(start, offset - start);
return this.getTemplate().getName().toLowerCase().startsWith(content.toLowerCase());
} catch (BadLocationException e) {
e.printStackTrace();
}
}
return super.validate(document, offset, event);
}
private String indentCssForXml(String formattedCssBlock, IDocument document,
TypedPosition partition, String cssLineSeparator, String xmlLineSeparator)
throws BadLocationException {
String oneXmlIndent = computeOneXmlIndentString();
int lineNumberInDocument = document.getLineOfOffset(partition.getOffset());
int offsetOfLineInDocument = document.getLineOffset(lineNumberInDocument);
String lineContents = document.get(offsetOfLineInDocument,
document.getLineLength(lineNumberInDocument));
int offsetOfNonwhitespaceInLine = StringUtilities.findNonwhitespaceCharacter(
lineContents, 0);
// The indent string that will be used for the closing tag </ui:style>
String styleElementIndentString;
// The indent string that will be used for to precede each line of the CSS
// block
String cssBlockIndentString;
if (offsetOfLineInDocument + offsetOfNonwhitespaceInLine == partition.getOffset()) {
// The CSS block is alone on this line, use whatever indentation it has
cssBlockIndentString = lineContents.substring(0,
offsetOfNonwhitespaceInLine);
styleElementIndentString = cssBlockIndentString.replace(oneXmlIndent, "");
} else {
// Something else is before the CSS block on this line (likely the style
// tag)
styleElementIndentString = lineContents.substring(0,
offsetOfNonwhitespaceInLine);
cssBlockIndentString = styleElementIndentString + oneXmlIndent;
}
return processCssBlock(formattedCssBlock, cssLineSeparator,
xmlLineSeparator, cssBlockIndentString, styleElementIndentString);
}
/**
* Look for first non-whitespace character on the line and move to it
*
* @see com.mulgasoft.emacsplus.commands.EmacsPlusNoEditHandler#transform(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection, org.eclipse.core.commands.ExecutionEvent)
*/
@Override
protected int transform(ITextEditor editor, IDocument document, ITextSelection currentSelection,
ExecutionEvent event) throws BadLocationException {
int offset = getCursorOffset(editor,currentSelection);
IRegion linfo = document.getLineInformationOfOffset(offset);
int llen = linfo.getLength();
String line = document.get(linfo.getOffset(), llen);
int index = 0;
while (index < llen && Character.isWhitespace(line.charAt(index))) {
index++;
}
int newOffset = linfo.getOffset() + index;
// don't move if we're at the same offset
if (newOffset != offset) {
// if past the last character on the line, set to end
if (index == llen) {
--index;
}
setCursorOffset(editor, newOffset);
if (isMarkEnabled(editor, currentSelection)) {
// set new point/mark selection when mark enabled
int mark = getMark(editor);
selectAndReveal(editor,newOffset,mark);
setFlagMark(mark - newOffset == 0);
}
}
return NO_OFFSET;
}
/**
* Gets line from document.
*
* @param i Line number
* @return String line in String form
*/
public static String getLine(IDocument doc, int i) {
try {
IRegion lineInformation = doc.getLineInformation(i);
return doc.get(lineInformation.getOffset(), lineInformation.getLength());
} catch (Exception e) {
return "";
}
}
public static String delim_to_cursor( IDocument doc, int offsetDelim, int offset){
String valToCursor = "";
try {
valToCursor = doc.get(offsetDelim, offset - offsetDelim);
} catch (BadLocationException ignore) {
ExceptionHandler.warn("lastWord: " + ignore);
}
return valToCursor;
}
private void smartIndentAfterOpeningBracket(IDocument d, DocumentCommand c) {
if (c.offset < 1 || d.getLength() == 0)
return;
JavaHeuristicScanner scanner= new JavaHeuristicScanner(d);
int p= (c.offset == d.getLength() ? c.offset - 1 : c.offset);
try {
// current line
int line= d.getLineOfOffset(p);
int lineOffset= d.getLineOffset(line);
// make sure we don't have any leading comments etc.
if (d.get(lineOffset, p - lineOffset).trim().length() != 0)
return;
// line of last Java code
int pos= scanner.findNonWhitespaceBackward(p, JavaHeuristicScanner.UNBOUND);
if (pos == -1)
return;
int lastLine= d.getLineOfOffset(pos);
// only shift if the last java line is further up and is a braceless block candidate
if (lastLine < line) {
JavaIndenter indenter= new JavaIndenter(d, scanner, fProject);
StringBuffer indent= indenter.computeIndentation(p, true);
String toDelete= d.get(lineOffset, c.offset - lineOffset);
if (indent != null && !indent.toString().equals(toDelete)) {
c.text= indent.append(c.text).toString();
c.length += c.offset - lineOffset;
c.offset= lineOffset;
}
}
} catch (BadLocationException e) {
JavaPlugin.log(e);
}
}
/**
* Extracts a CSS block from a document and creates the CSS model and
* documents.
* <p>
* This requires a model loader to create the model, instead of taking in an
* empty model. The reason is, there are multiple ways to create a model, but
* we need a way that also creates the associated documents. The model loader
* is guaranteed to do this, which is why we specifically take this as a
* parameter.
*
* @param cssModelLoader a model loader used to create the empty model (see
* {@link com.google.gwt.eclipse.core.uibinder.sse.css.model.CssResourceAwareModelLoader}
* )
* @return a container for the model and associated documents, or null if it
* could not be successfully extracted
*/
public static GssExtractor extract(IDocument document, int offset,
int length, CSSModelLoader cssModelLoader) {
try {
String cssBlock = document.get(offset, length);
return extract(cssBlock, cssModelLoader);
} catch (BadLocationException e) {
// Likely not to happen, but in case it does, return null
return null;
}
}
/**
* Computes the token at the given <code>offset</code> in <code>document</code>
* to emphasize the ranges matching this token in proposal's display string.
*
* @param document
* the document where content assist is invoked
* @param offset
* the offset in the document at current caret location
* @return the token at the given <code>offset</code> in <code>document</code>
* to be used for emphasizing matching ranges in proposal's display
* string
* @since 3.12
*/
protected String getPatternToEmphasizeMatch(IDocument document, int offset) {
int start = getPrefixCompletionStart(document, offset);
int patternLength = offset - start;
String pattern = null;
try {
pattern = document.get(start, patternLength);
} catch (BadLocationException e) {
// return null
}
return pattern;
}