簡體   English   中英

如何在當前打開的編輯器中用另一個String替換特定的String?

[英]How to replace a particular String with another String in the currently opened editor?

嗨,我正在做一個eclipse插件項目來創建一個IDE。 在我的IDE中,將檢查當前打開的編輯器是否包含特定字符串,並應將其替換為在側視圖中的文本框中輸入的字符串。 我可以訪問編輯器,但是如果我搜索特定的字符串並將該字符串替換為用戶輸入的輸入,則它將不起作用。

 IDocumentProvider provider=((AbstractTextEditor) ieditorpart).getDocumentProvid();
 IDocument doc = provider.getDocument(ieditorpart.getEditorInput());  
 String content = doc.get();
 pos=content.compareTo("\\/\\*ProbeEnd\\*\\/");
 doc.replace(pos,5, "hello");

但這是行不通的...在這里,我只是給替換字符串打招呼,但該值應從文本框中獲取。

訪問編輯器是否有任何錯誤? 我應該使用這種方法執行此操作還是有任何方法可以執行此操作? 有人可以幫我嗎?

為什么變量“ pos”是compareTo-Value(-1,0,1)? compareTo返回兩個字符串的字典順序。

IDocument的替換方法具有三個參數:

  • int offset-文檔中的偏移量,應在其中插入“文本”
  • int length-從“偏移”開始的長度,應將其覆蓋。 長度0表示插入。
  • 字符串文本-替代文本

例:

String oldContent = doc.get();
assert oldContent.equals("TestingText");

String replaceText = "REPLACE";

doc.replace(5,3,replaceText);

String newContent = doc.get();
assert newContent.equals("TestiREPLACEext");
//offset 5 is the position after 'Testi'
//length 3 means 'ngT' (starting from the offset) should be replaced
//REPLACE is the newText

從您的編輯器調用firePropertyChange(IEditorPart.PROP_INPUT)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM