liteide icon indicating copy to clipboard operation
liteide copied to clipboard

add repace all within selected text function

Open epowsal opened this issue 2 years ago • 3 comments
trafficstars

this is the replace all within selected text function source code: void FindEditor::replaceSelAll() { if (!m_option.isValid()) { return; }

m_option.backWard = false;

LiteApi::IEditor *editor = m_liteApp->editorManager()->currentEditor();
if (!editor) {
    return;
}
LiteApi::ITextEditor *textEditor = LiteApi::getTextEditor(editor);
if (!textEditor) {
    return;
}


QPlainTextEdit *ed = LiteApi::getPlainTextEdit(editor);
if (!ed) {
    return;
}
QTextCursor cursor = ed->textCursor();
int  repcnt=0;
if ( cursor.hasSelection() ) {
    QString text = cursor.selectedText();
	 if (m_option.matchCase) {
		repcnt=text.count(m_findEdit->text(), Qt::CaseSensitive);
	 }else{
		 repcnt=text.count(m_findEdit->text(), Qt::CaseInsensitive);
	 }
}
//m_status->setText(QString("Replace:%1").arg(repcnt));
replaceHelper(textEditor,&m_option,repcnt);

}

epowsal avatar Jan 24 '23 01:01 epowsal

you can create PR

visualfc avatar Jan 24 '23 03:01 visualfc

import javax.swing.*;

public class ReplaceAllSelected { public static void main(String[] args) { JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea);

  String find = JOptionPane.showInputDialog(null, "Enter text to find:");
  String replace = JOptionPane.showInputDialog(null, "Enter replacement text:");

  // Get the selected text
  String selectedText = textArea.getSelectedText();

  // Check if any text is selected
  if (selectedText != null) {
     // Replace all occurrences of the find text with the replace text within the selected text
     String replacedText = selectedText.replaceAll(find, replace);

     // Replace the selected text with the replaced text
     textArea.replaceRange(replacedText, textArea.getSelectionStart(), textArea.getSelectionEnd());
  } else {
     JOptionPane.showMessageDialog(null, "No text is selected.");
  }

} }

This code uses the JTextArea class to create a text area, and the JScrollPane class to add scrollbars to the text area. It uses the JOptionPane class to display dialogs for the user to enter the text to find and the replacement text. The code then gets the selected text from the text area using the getSelectedText() method. If there is selected text, it replaces all occurrences of the find text with the replacement text within the selected text using the replaceAll() method, and replaces the selected text with the replaced text using the replaceRange() method. If no text is selected, it displays a message dialog to inform the user.

Starx-Quantum avatar Jan 29 '23 07:01 Starx-Quantum

STarxer

Starx-Quantum avatar Jan 29 '23 07:01 Starx-Quantum