⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buffer.java

📁 开源的java 编辑器源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	} //}}}	//{{{ setBooleanProperty() method	/**	 * Sets a boolean property.	 * @param name The property name	 * @param value The value	 * @since jEdit 4.0pre1	 */	public void setBooleanProperty(String name, boolean value)	{		setProperty(name,value ? Boolean.TRUE : Boolean.FALSE);	} //}}}	//{{{ getIntegerProperty() method	/**	 * Returns the value of an integer property. This method is thread-safe.	 * @param name The property name	 * @since jEdit 4.0pre1	 */	public int getIntegerProperty(String name, int defaultValue)	{		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 defaultValue;		else if(obj instanceof Number)			return ((Number)obj).intValue();		else		{			try			{				int returnValue = Integer.parseInt(					obj.toString().trim());				properties.put(name,new PropValue(					new Integer(returnValue),					defaultValueFlag));				return returnValue;			}			catch(Exception e)			{				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 RE getRegexpProperty(String name, int cflags,		RESyntax syntax) throws REException	{		synchronized(propertyLock)		{			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 = jEdit.getMode(rules.getModeName())				.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 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 name	 * @since jEdit 4.2pre1	 */	public void setMode(String mode)	{		setMode(jEdit.getMode(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;		textMode = "text".equals(mode.getName());		setTokenMarker(mode.getTokenMarker());		resetCachedProperties();		propertiesChanged();	} //}}}	//{{{ 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);		// this must be in reverse order so that modes from the user		// catalog get checked first!		for(int i = modes.length - 1; i >= 0; 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();		if(lineIndex < 0 || lineIndex >= lineMgr.getLineCount())			throw new ArrayIndexOutOfBoundsException(lineIndex);		int firstInvalidLineContext = lineMgr.getFirstInvalidLineContext();		int start;		if(textMode || firstInvalidLineContext == -1)		{			start = lineIndex;		}		else		{			start = Math.min(firstInvalidLineContext,				lineIndex);		}		if(Debug.TOKEN_MARKER_DEBUG)			Log.log(Log.DEBUG,this,"tokenize from " + start + " to " + lineIndex);		TokenMarker.LineContext oldContext = null;		TokenMarker.LineContext context = null;		for(int i = start; i <= lineIndex; i++)		{			getLineText(i,seg);			oldContext = lineMgr.getLineContext(i);			TokenMarker.LineContext prevContext = (				(i == 0 || textMode) ? null				: lineMgr.getLineContext(i - 1)			);			context = tokenMarker.markTokens(prevContext,				(i == lineIndex ? tokenHandler				: DummyTokenHandler.INSTANCE),seg);			lineMgr.setLineContext(i,context);		}		int lineCount = lineMgr.getLineCount();		if(lineCount - 1 == lineIndex)			lineMgr.setFirstInvalidLineContext(-1);		else if(oldContext != context)			lineMgr.setFirstInvalidLineContext(lineIndex + 1);		else if(firstInvalidLineContext == -1)			/* do nothing */;		else		{			lineMgr.setFirstInvalidLineContext(Math.max(				firstInvalidLineContext,lineIndex + 1));		}	} //}}}	//}}}	//{{{ 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();		}	} //}}}	//{{{ indentLines() method	/**	 * Indents all specified lines.	 * @param start The first line to indent	 * @param end The last line to indent	 * @since jEdit 3.1pre3	 */	public void indentLines(int start, int end)	{		try		{			beginCompoundEdit();			for(int i = start; i <= end; i++)				indentLine(i,true);		}		finally		{			endCompoundEdit();		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -