2012-09-04 22 views
6

के साथ एक नया ग्रहण संपादक कैसे खोलें मैं उपरोक्त प्रोग्रामेटिक रूप से करना चाहता हूं।

मैंने How to get cursor position in an eclipse TextEditor और Eclipse-plugin how to get current text editor corsor position पर देखा तो मुझे पता है कि वर्तमान खुले संपादक से कर्सर ऑफ़सेट कैसे प्राप्त होता है। हालांकि, मैं कर्सर ऑफ़सेट को एक नए संपादक में सेट करने की कोशिश कर रहा हूं जो प्रोग्राम द्वारा मेरे द्वारा प्रोग्राम किया गया है।
एक विशिष्ट कर्सर ऑफसेट स्थिति

तरह से मैं इस समय अपने नए संपादक को खोलने कर रहा हूँ इस प्रकार है:

IWorkbenchWindow win = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 
    IWorkbenchPage page = win.getActivePage(); 
    if (page != null) { 
     IEditorPart editor = page.getActiveEditor(); 
     if (editor != null) { 
      IEditorInput input = editor.getEditorInput(); 
      if (input instanceof IFileEditorInput) { 
       String fileLocation = ((IFileEditorInput) input).getFile().getLocation().toOSString(); 
       String newFileLocartion = generateNewFileLocation(fileLocation); 
       File file = new File(newFileLocartion); 
       IFileStore fileStore = EFS.getLocalFileSystem().getStore(file.toURI()); 
       try { 
        IDE.openEditorOnFileStore(page, fileStore); 
       } catch (PartInitException e) { 
        // TODO error handling 
       } 
      } 
     } 
    } 

एक विशिष्ट ऑफसेट में खोलने के लिए नए संपादक सेट खोलने के लिए एक रास्ता है (यह मानते हुए मैं पहले से ही जानता है में ऑफसेट अग्रिम)?

धन्यवाद!

उत्तर

3

यह फ़ाइल में निर्दिष्ट पंक्ति पर नेविगेट करने के लिए इस स्निपेट का उपयोग करता है।

public static void navigateToLine(IFile file, Integer line) 
{ 
    HashMap<String, Object> map = new HashMap<String, Object>(); 
    map.put(IMarker.LINE_NUMBER, line); 
    IMarker marker = null; 
    try { 
     marker = file.createMarker(IMarker.TEXT); 
     marker.setAttributes(map); 
     try { 
      IDE.openEditor(getActivePage(), marker); 
     } catch (PartInitException e) { 
      //complain 
     } 
    } catch (CoreException e1) { 
     //complain 
    } finally { 
     try { 
      if (marker != null) 
       marker.delete(); 
     } catch (CoreException e) { 
      //whatever 
     } 
    } 
} 

शायद आपको बिल्कुल वही नहीं चाहिए जो आपको चाहिए, लेकिन उपयोगी हो सकता है। (// शिकायत त्रुटि प्रबंधन कोड को प्रतिस्थापित करती है जो उत्पाद के लिए विशिष्ट थी जहां इसका उपयोग किया जाता है)

2

मैंने निम्नलिखित का उपयोग किया, जो पिछले उत्तर की तुलना में अधिक सरल है। मान लें कि आप int offset, IWorkbenchPage page और IFile file है (और ऐसा लगता है कि वे सब ओपी के सवाल में मौजूद):

ITextEditor editor = (ITextEditor) IDE.openEditor(page, file); 
editor.selectAndReveal(offset, 0); 

मुझे पता चला कैसे this answer. (से ऐसा करने के लिए, लेकिन करने के लिए selectAndReveal का दूसरा पैरामीटर की स्थापना द्वारा शून्य, कोई टेक्स्ट हाइलाइट नहीं किया गया है)

संबंधित मुद्दे