📄 buffer.java
字号:
{ return defaultValue; } } } //}}} //{{{ setIntegerProperty() method /** * Sets an integer property. * @param name The property name * @param value The value * @since jEdit 4.0pre1 */ public void setIntegerProperty(String name, int value) { setProperty(name,new Integer(value)); } //}}} //{{{ getRegexpProperty() method /** * Returns the value of a property as a regular expression. * This method is thread-safe. * @param name The property name * @param cflags Regular expression compilation flags * @param syntax Regular expression syntax * @since jEdit 4.1pre9 */ public synchronized RE getRegexpProperty(String name, int cflags, RESyntax syntax) throws REException { boolean defaultValueFlag; Object obj; PropValue value = (PropValue)properties.get(name); if(value != null) { obj = value.value; defaultValueFlag = value.defaultValue; } else { obj = getProperty(name); // will be cached from now on... defaultValueFlag = true; } if(obj == null) return null; else if(obj instanceof RE) return (RE)obj; else { RE re = new RE(obj.toString(),cflags,syntax); properties.put(name,new PropValue(re, defaultValueFlag)); return re; } } //}}} //{{{ getRuleSetAtOffset() method /** * Returns the syntax highlighting ruleset at the specified offset. * @since jEdit 4.1pre1 */ public ParserRuleSet getRuleSetAtOffset(int offset) { int line = getLineOfOffset(offset); offset -= getLineStartOffset(line); if(offset != 0) offset--; DefaultTokenHandler tokens = new DefaultTokenHandler(); markTokens(line,tokens); Token token = TextUtilities.getTokenAtOffset(tokens.getTokens(),offset); return token.rules; } //}}} //{{{ getKeywordMapAtOffset() method /** * Returns the syntax highlighting keyword map in effect at the * specified offset. Used by the <b>Complete Word</b> command to * complete keywords. * @param offset The offset * @since jEdit 4.0pre3 */ public KeywordMap getKeywordMapAtOffset(int offset) { return getRuleSetAtOffset(offset).getKeywords(); } //}}} //{{{ getContextSensitiveProperty() method /** * Some settings, like comment start and end strings, can * vary between different parts of a buffer (HTML text and inline * JavaScript, for example). * @param offset The offset * @param name The property name * @since jEdit 4.0pre3 */ public String getContextSensitiveProperty(int offset, String name) { ParserRuleSet rules = getRuleSetAtOffset(offset); Object value = null; Hashtable rulesetProps = rules.getProperties(); if(rulesetProps != null) value = rulesetProps.get(name); if(value == null) { value = rules.getMode().getProperty(name); if(value == null) value = mode.getProperty(name); } if(value == null) return null; else return String.valueOf(value); } //}}} //{{{ Used to store property values static class PropValue { PropValue(Object value, boolean defaultValue) { if(value == null) throw new NullPointerException(); this.value = value; this.defaultValue = defaultValue; } Object value; /** * If this is true, then this value is cached from the mode * or global defaults, so when the defaults change this property * value must be reset. */ boolean defaultValue; /** * For debugging purposes. */ public String toString() { return value.toString(); } } //}}} //{{{ toggleWordWrap() method /** * Toggles word wrap between the three available modes. This is used * by the status bar. * @param view We show a message in the view's status bar * @since jEdit 4.1pre3 */ public void toggleWordWrap(View view) { String wrap = getStringProperty("wrap"); if(wrap.equals("none")) wrap = "soft"; else if(wrap.equals("soft")) wrap = "hard"; else if(wrap.equals("hard")) wrap = "none"; view.getStatus().setMessageAndClear(jEdit.getProperty( "view.status.wrap-changed",new String[] { wrap })); setProperty("wrap",wrap); propertiesChanged(); } //}}} //{{{ toggleLineSeparator() method /** * Toggles the line separator between the three available settings. * This is used by the status bar. * @param view We show a message in the view's status bar * @since jEdit 4.1pre3 */ public void toggleLineSeparator(View view) { String status = null; String lineSep = getStringProperty("lineSeparator"); if("\n".equals(lineSep)) { status = "windows"; lineSep = "\r\n"; } else if("\r\n".equals(lineSep)) { status = "mac"; lineSep = "\r"; } else if("\r".equals(lineSep)) { status = "unix"; lineSep = "\n"; } view.getStatus().setMessageAndClear(jEdit.getProperty( "view.status.linesep-changed",new String[] { jEdit.getProperty("lineSep." + status) })); setProperty("lineSeparator",lineSep); setDirty(true); propertiesChanged(); } //}}} //}}} //{{{ Edit modes, syntax highlighting //{{{ getMode() method /** * Returns this buffer's edit mode. This method is thread-safe. */ public final Mode getMode() { return mode; } //}}} //{{{ setMode() method /** * Sets this buffer's edit mode. Note that calling this before a buffer * is loaded will have no effect; in that case, set the "mode" property * to the name of the mode. A bit inelegant, I know... * @param mode The mode */ public void setMode(Mode mode) { /* This protects against stupid people (like me) * doing stuff like buffer.setMode(jEdit.getMode(...)); */ if(mode == null) throw new NullPointerException("Mode must be non-null"); this.mode = mode; propertiesChanged(); // sets up token marker } //}}} //{{{ setMode() method /** * Sets this buffer's edit mode by calling the accept() method * of each registered edit mode. */ public void setMode() { String userMode = getStringProperty("mode"); if(userMode != null) { Mode m = jEdit.getMode(userMode); if(m != null) { setMode(m); return; } } String nogzName = name.substring(0,name.length() - (name.endsWith(".gz") ? 3 : 0)); Mode[] modes = jEdit.getModes(); String firstLine = getLineText(0); for(int i = 0; i < modes.length; i++) { if(modes[i].accept(nogzName,firstLine)) { setMode(modes[i]); return; } } Mode defaultMode = jEdit.getMode(jEdit.getProperty("buffer.defaultMode")); if(defaultMode == null) defaultMode = jEdit.getMode("text"); setMode(defaultMode); } //}}} //{{{ markTokens() method /** * Returns the syntax tokens for the specified line. * @param lineIndex The line number * @param tokenHandler The token handler that will receive the syntax * tokens * @since jEdit 4.1pre1 */ public void markTokens(int lineIndex, TokenHandler tokenHandler) { Segment seg; if(SwingUtilities.isEventDispatchThread()) seg = this.seg; else seg = new Segment(); try { writeLock(); if(lineIndex < 0 || lineIndex >= offsetMgr.getLineCount()) throw new ArrayIndexOutOfBoundsException(lineIndex); /* * Scan backwards, looking for a line with * a valid line context. */ int start; if(textMode) { start = lineIndex; } else { start = 0; for(int i = lineIndex - 1; i >= 0; i--) { if(offsetMgr.isLineContextValid(i)) { start = i; break; } } } //System.err.println("tokenize from " + start + " to " + lineIndex); for(int i = start; i <= lineIndex; i++) { getLineText(i,seg); TokenMarker.LineContext context = offsetMgr.getLineContext(i); ParserRule oldRule; ParserRuleSet oldRules; if(context == null) { //System.err.println(i + ": null context"); oldRule = null; oldRules = null; } else { oldRule = context.inRule; oldRules = context.rules; } // this should be null if the line in question does // not have a valid context because we don't want // to inherit the inRule and rules attributes from // an invalid line. TokenMarker.LineContext prevContext = ( (i == 0 || !offsetMgr.isLineContextValid(i - 1)) ? null : offsetMgr.getLineContext(i - 1) ); context = tokenMarker.markTokens(prevContext, (i == lineIndex ? tokenHandler : DummyTokenHandler.INSTANCE),seg); offsetMgr.setLineContext(i,context); // Could incorrectly be set to 'false' with // recursive delegates, where the chaining might // have changed but not the rule set in question (?) if(oldRule != context.inRule) { //System.err.println(i + ": rules don't match" + oldRule + "," + context.inRule); nextLineRequested = true; } else if(oldRules != context.rules) { //System.err.println(i + ": rule sets don't match: " + oldRules + "," + context.rules); nextLineRequested = true; } //else if(i != lastTokenizedLine) // nextLineRequested = false; } int lineCount = offsetMgr.getLineCount(); if(nextLineRequested && lineCount - lineIndex > 1) { offsetMgr.lineInfoChangedFrom(lineIndex + 1); } } finally { writeUnlock(); } } //}}} //{{{ isNextLineRequested() method /** * Returns true if the next line should be repainted. This * will return true after a line has been tokenized that starts * a multiline token that continues onto the next line. */ public boolean isNextLineRequested() { boolean retVal = nextLineRequested; nextLineRequested = false; return retVal; } //}}} //{{{ getTokenMarker() method /** * This method is only public so that the <code>OffsetManager</code> * class can use it. * @since jEdit 4.0pre1 */ public TokenMarker getTokenMarker() { return tokenMarker; } //}}} //}}} //{{{ Indentation //{{{ removeTrailingWhiteSpace() method /** * Removes trailing whitespace from all lines in the specified list. * @param lines The line numbers * @since jEdit 3.2pre1 */ public void removeTrailingWhiteSpace(int[] lines) { try { beginCompoundEdit(); for(int i = 0; i < lines.length; i++) { int pos, lineStart, lineEnd, tail; getLineText(lines[i],seg); // blank line if (seg.count == 0) continue; lineStart = seg.offset; lineEnd = seg.offset + seg.count - 1; for (pos = lineEnd; pos >= lineStart; pos--) { if (!Character.isWhitespace(seg.array[pos])) break; } tail = lineEnd - pos; // no whitespace if (tail == 0) continue; remove(getLineEndOffset(lines[i]) - 1 - tail,tail); } } finally { endCompoundEdit(); } } //}}} //{{{ shiftIndentLeft() method /** * Shifts the indent of each line in the specified list to the left. * @param lines The line numbers * @since jEdit 3.2pre1 */ public void shiftIndentLeft(int[] lines) { int tabSize = getTabSize(); int indentSize = getIndentSize(); boolean noTabs = getBooleanProperty("noTabs"); try { beginCompoundEdit(); for(int i = 0; i < lines.length; i++) { int lineStart = getLineStartOffset(lines[i]); String line = getLineText(lines[i]); int whiteSpace = MiscUtilities .getLeadingWhiteSpace(line); if(whiteSpace == 0) continue; int whiteSpaceWidth = Math.max(0,MiscUtilities .getLeadingWhiteSpaceWidth(line,tabSize) - indentSize); insert(lineStart + whiteSpace,MiscUtilities .createWhiteSpace(whiteSpaceWidth, (noTabs ? 0 : tabSize))); remove(lineStart,whiteSpace); } } finally { endCompoundEdit(); } } //}}} //{{{ shiftIndentRight() method /** * Shifts the indent of each line in the specified list to the right. * @param lines The line numbers * @since jEdit 3.2pre1 */ public void shiftIndentRight(int[] lines) { try { beginCompoundEdit(); int tabSize = getTabSize(); int indentSize = getIndentSize(); boolean noTabs = getBooleanProperty("noTabs"); for(int i = 0; i < lines.length; i++) { int lineStart = getLineStartOffset(lines[i]); String line = getLineText(lines[i]); int whiteSpace = MiscUtilities .getLeadingWhiteSpace(line); // silly usability hack //if(lines.length != 1 && whiteSpace == 0) // continue; int whiteSpaceWidth = MiscUtilities .getLeadingWhiteSpaceWidth( line,tabSize) + indentSize; insert(lineStart + whiteSpace,MiscUtilities .createWhiteSpace(whiteSpaceWidth, (noTabs ? 0 : tabSize))); remove(lineStart,whiteSpace); } } finally { endCompoundEdit(); } } //}}} //{{{ indentLine() method /** * If auto indent is enabled, this method is called when the `Tab' * or `Enter' key is pressed to perform mode-specific indentation * and return true, or return false if a normal tab is to be inserted. * @param line The line number to indent * @param canIncreaseIndent If false, nothing will be done if the * calculated indent is greater than the current * @param canDecreaseIndent If false, nothing will be done if the * calculated indent is less than the current * @return true if the tab key event should be swallowed (ignored) * false if a real tab should be inserted */ public boolean indentLine(int lineIndex, boolean canIncreaseIndent, boolean canDecreaseIndent) { getLineText(lineIndex,seg); int tabSize = getTabSize(); int whitespaceChars = 0; int currentIndent = 0;loop: for(int i = 0; i < seg.count; i++) { char c = seg.array[seg.offset + i]; switch(c) { case ' ': currentIndent++; whitespaceChars++; break; case '\t': currentIndent += (tabSize - (currentIndent % tabSize)); whitespaceChars++; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -