[英]Eclipse Plugin Get Code from Current Open File
如何從String
或String[]
返回的Eclipse中的當前打開文件中獲取代碼? 我需要這個用於我正在制作的插件。
假設我有以下代碼:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
如果我打開HelloWorld.java,如何在String[]
返回該代碼? String[]
將包含:
要獲取當前編輯的文件內容,您可以執行以下操作:
IWorkbenchPart workbenchPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
if (file == null) throw new FileNotFoundException();
String content = IOUtils.toString(file.getContents(), file.getCharset());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.