Просмотр исходного кода

Replace and Replace All for IDE text editor

Ivan Safrin 13 лет назад
Родитель
Сommit
e7a3abcb94

+ 7 - 8
Core/Contents/Source/PolyString.cpp

@@ -150,14 +150,13 @@ vector<String> String::split(const String &delim) const {
 }
 
 String String::replace(const String &what, const String &withWhat) const {
-	vector<String> arr = split(what);
-	String retString = "";
-	for(int i= 0; i < arr.size(); i++) {
-		retString += arr[i];
-		if(i < arr.size()-1)
-			retString += withWhat;
-	}
-	
+
+	size_t pos = 0;
+	std::string retString = contents;
+	while((pos = retString.find(what.contents, pos)) != std::string::npos) {
+		retString.replace(pos, what.length(), withWhat.contents);
+		pos += withWhat.length();
+	 }
 	return retString;
 }
 

+ 7 - 0
IDE/Contents/Include/PolycodeTextEditor.h

@@ -34,8 +34,14 @@ class FindBar : public UIElement {
 		~FindBar();
 		
 		void setBarWidth(int width);
+		void onKeyDown(PolyKEY key, wchar_t charCode);
 		
 		UITextInput *findInput;
+		UITextInput *replaceInput;		
+		UIImageButton *closeButton;		
+		
+		UIImageButton *replaceAllButton;
+		
 	protected:
 		ScreenShape *barBg;
 		
@@ -84,6 +90,7 @@ protected:
 
 	PolycodeSyntaxHighlighter *syntaxHighligher;
 	UITextInput *textInput;
+	
 };
 
 class PolycodeTextEditorFactory : public PolycodeEditorFactory {

BIN
IDE/Contents/Resources/Images/barClose.png


BIN
IDE/Contents/Resources/Images/replaceAll.png


+ 66 - 2
IDE/Contents/Source/PolycodeTextEditor.cpp

@@ -38,7 +38,7 @@ PolycodeSyntaxHighlighter::PolycodeSyntaxHighlighter(String extension) {
 	separators = String("[ [ ] { } ; . , : # ( ) \t \n = + - / \\ ' \"").split(" ");
 	separators.push_back(" ");
 	
-	keywords = String("require true false class self break do end else elseif function if local nil not or repeat return then until while").split(" ");
+	keywords = String("and require true false class self break do end else elseif function if local nil not or repeat return then until while").split(" ");
 }
 
 PolycodeSyntaxHighlighter::~PolycodeSyntaxHighlighter() {
@@ -217,6 +217,11 @@ bool PolycodeTextEditor::openFile(OSFileEntry filePath) {
 	
 	findBar->findInput->addEventListener(this, Event::COMPLETE_EVENT);
 	findBar->findInput->addEventListener(this, Event::CANCEL_EVENT);
+	findBar->replaceInput->addEventListener(this, Event::CANCEL_EVENT);
+	findBar->replaceInput->addEventListener(this, Event::COMPLETE_EVENT);
+			
+	findBar->closeButton->addEventListener(this, UIEvent::CLICK_EVENT);
+	findBar->replaceAllButton->addEventListener(this, UIEvent::CLICK_EVENT);
 		
 	syntaxHighligher = NULL;
 	
@@ -236,6 +241,35 @@ bool PolycodeTextEditor::openFile(OSFileEntry filePath) {
 }
 
 void PolycodeTextEditor::handleEvent(Event *event) {
+
+	if(event->getDispatcher() == findBar->replaceAllButton) {
+		if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CLICK_EVENT) {
+			textInput->replaceAll(findBar->findInput->getText(), findBar->replaceInput->getText());
+		}
+	}
+
+
+	if(event->getDispatcher() == findBar->closeButton) {
+		if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CLICK_EVENT) {
+			hideFindBar();
+		}
+	}
+
+	if(event->getDispatcher() == findBar->replaceInput) {
+		if(event->getEventType() == "Event") {
+		
+			if(event->getEventCode() == Event::CANCEL_EVENT) {
+				hideFindBar();
+			}
+					
+			if(event->getEventCode() == Event::COMPLETE_EVENT) {
+				textInput->findString(findBar->findInput->getText(), true, findBar->replaceInput->getText());
+			}
+			
+		}
+	}	
+	
+	
 	if(event->getDispatcher() == findBar->findInput) {
 		if(event->getEventType() == "Event") {
 		
@@ -262,6 +296,7 @@ void PolycodeTextEditor::handleEvent(Event *event) {
 void PolycodeTextEditor::showFindBar() {
 	findBar->visible = true;
 	findBar->focusChild(findBar->findInput);
+	findBar->findInput->selectAll();
 	Resize(editorSize.x, editorSize.y);
 }
 
@@ -308,10 +343,38 @@ FindBar::FindBar() : UIElement() {
 	addChild(findLabel);
 	findLabel->setColor(0.0, 0.0, 0.0, 0.3);
 	findLabel->setPosition(10,4);
+
+	ScreenLabel *replaceLabel = new ScreenLabel("Replace:", 16);
+	addChild(replaceLabel);
+	replaceLabel->setColor(0.0, 0.0, 0.0, 0.3);
+	replaceLabel->setPosition(200,4);
+
+	processInputEvents = true;
 	
-	findInput = new UITextInput(false, 100, 12);
+	findInput = new UITextInput(false, 120, 12);
 	addChild(findInput);
 	findInput->setPosition(60, 4);
+
+	replaceInput = new UITextInput(false, 120, 12);
+	addChild(replaceInput);
+	replaceInput->setPosition(280, 4);
+	
+	replaceAllButton = new UIImageButton("Images/replaceAll.png");
+	addChild(replaceAllButton);
+	replaceAllButton->setPosition(420, 5);
+	
+	closeButton = new UIImageButton("Images/barClose.png");
+	addChild(closeButton);
+}
+
+void FindBar::onKeyDown(PolyKEY key, wchar_t charCode) {
+	if(key == KEY_TAB) {
+		focusNextChild();
+	}
+	
+	if(key == KEY_ESCAPE) {
+	
+	}	
 }
 
 FindBar::~FindBar(){
@@ -320,4 +383,5 @@ FindBar::~FindBar(){
 
 void FindBar::setBarWidth(int width) {
 	barBg->setShapeSize(width, 30);
+	closeButton->setPosition(width - 30, 5);
 }

+ 3 - 1
Modules/Contents/UI/Include/PolyUITextInput.h

@@ -102,7 +102,9 @@ namespace Polycode {
 			void Copy();
 			void Paste();
 			
-			void findString(String stringToFind);
+			void replaceAll(String what, String withWhat);
+			
+			void findString(String stringToFind, bool replace=false, String replaceString="");
 			void findNext();
 			void findPrevious();
 			void findCurrent();

+ 34 - 1
Modules/Contents/UI/Source/PolyUITextInput.cpp

@@ -140,6 +140,7 @@ UITextInput::UITextInput(bool multiLine, Number width, Number height) : UIElemen
 		addChild(scrollContainer);
 	} else {
 		addChild(linesContainer);
+		enableScissor = true;
 	}
 	
 	undoStateIndex = 0;
@@ -164,6 +165,11 @@ void UITextInput::clearSelection() {
 
 void UITextInput::setSelection(int lineStart, int lineEnd, int colStart, int colEnd) {
 
+	if(lineStart == lineEnd && colStart == colEnd) {
+		clearSelection();
+		return;
+	}
+
 	if(lineStart == lineOffset) {
 		selectionLine = lineEnd;
 	} else {
@@ -598,7 +604,16 @@ void UITextInput::selectWordAtCaret() {
 	updateCaretPosition();	
 }
 
-void UITextInput::findString(String stringToFind) {
+void UITextInput::replaceAll(String what, String withWhat) {
+	for(int i=0; i < lines.size(); i++) {
+		lines[i]->setText(lines[i]->getText().replace(what, withWhat));
+	}
+	needFullRedraw  = true;
+	changedText();
+}
+
+void UITextInput::findString(String stringToFind, bool replace, String replaceString) {
+
 	clearSelection();
 	findMatches.clear();
 	
@@ -623,7 +638,19 @@ void UITextInput::findString(String stringToFind) {
 		
 	}
 	
+	
 	if(findMatches.size() > 0) {
+
+		if(replace) {
+			FindMatch match = findMatches[findIndex];
+			String oldText = lines[match.lineNumber]->getText();
+			String newText = oldText.substr(0,match.caretStart) + replaceString + oldText.substr(match.caretEnd);
+			
+			lines[match.lineNumber]->setText(newText);
+			findMatches[findIndex].caretEnd = findMatches[findIndex].caretStart + replaceString.length();
+			changedText();			
+		}
+	
 		findIndex = 0;
 		findCurrent();
 	}
@@ -1143,6 +1170,12 @@ void UITextInput::onKeyDown(PolyKEY key, wchar_t charCode) {
 }
 
 void UITextInput::Update() {
+
+	if(!multiLine) {
+		Vector2 pos = getScreenPosition();
+		scissorBox.setRect(pos.x,pos.y, width, height);		
+	}
+
 	if(hasSelection) {
 		blinkerRect->visible = false;
 	}