簡體   English   中英

Eclipse插件從當前打開的文件中獲取代碼

[英]Eclipse Plugin Get Code from Current Open File

如何從StringString[]返回的Eclipse中的當前打開文件中獲取代碼? 我需要這個用於我正在制作的插件。

假設我有以下代碼:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

如果我打開HelloWorld.java,如何在String[]返回該代碼? String[]將包含:

  • “公共課HelloWorld {”
  • “public static void main(String [] args){”
  • “System.out.println(”Hello,world!“);”
  • “}”
  • “}”

要獲取當前編輯的文件內容,您可以執行以下操作:

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.

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