2013-04-15 5 views
6

के साथ प्रोग्रामेटिक रूप से फॉर्मेटिंग स्रोत कोड मैं जेडीटी के साथ कुछ कक्षाएं उत्पन्न कर रहा हूं। बाद में मैं पूरे ICompilationUnit को प्रारूपित करना चाहता हूं, जैसे कि मैंने चयन के बिना एक खुले संपादक में Ctrl + Shift + F (Source> Format) दबाया।जेडीटी

जेडीटी में एपीआई के लिए किसी भी पॉइंटर्स को प्रोग्राम कोड को प्रारूपित करने के लिए अत्यधिक सराहना की जाती है।

अतिरिक्त: मैंने इसे इस तरह से आजमाया, लेकिन कोड नहीं बदला गया है। मैं क्या कर रहा हूँ?

private void formatUnitSourceCode(ICompilationUnit targetUnit, IProgressMonitor monitor) throws JavaModelException { 
    CodeFormatter formatter = ToolFactory.createCodeFormatter(null); 
    TextEdit formatEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, targetUnit.getSource(), 0, targetUnit.getSource().length(), 0, null); 
    targetUnit.applyTextEdit(formatEdit, monitor); 
} 

उत्तर

5

यह एक बग हो सकता है, लेकिन एलिससे 4.2.2 में जेडीके का उपयोग करके, फ़ाइल में टेक्स्ट एडिट लागू करने के लिए आईसीओम्पिलेशन यूनिट की एक कार्य प्रति बनाना आवश्यक है।

targetUnit.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); 
    ... do work on the source file ... 
    formatUnitSourceCode(targetUnit, new SubProgressMonitor(monitor, 1)); 
    targetUnit.commitWorkingCopy(true, new SubProgressMonitor(monitor, 1)); 

स्वरूपण ही इस तरह से किया जाता है:

public static void formatUnitSourceCode(ICompilationUnit unit, IProgressMonitor monitor) throws JavaModelException { 
    CodeFormatter formatter = ToolFactory.createCodeFormatter(null); 
    ISourceRange range = unit.getSourceRange(); 
    TextEdit formatEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, unit.getSource(), range.getOffset(), range.getLength(), 0, null); 
    if (formatEdit != null && formatEdit.hasChildren()) { 
     unit.applyTextEdit(formatEdit, monitor); 
    } else { 
     monitor.done(); 
    } 
} 
2

जब generating some classes by using JDT, आप अपने स्रोत कोड में "\ t" रों डाल सकते हैं। या कोड फ़ॉर्मेटर का उपयोग करके, आपने जो किया है उसकी तरह। मैं निम्नलिखित कोड का परीक्षण किया है:

public static void main(String[] args) 
{ 
    String code = "public class TestFormatter{public static void main(String[] args){System.out.println(\"Hello World\");}}"; 
    CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null); 

    TextEdit textEdit = codeFormatter.format(CodeFormatter.K_UNKNOWN, code, 0,code.length(),0,null); 
    IDocument doc = new Document(code); 
    try { 
     textEdit.apply(doc); 
     System.out.println(doc.get()); 
    } catch (MalformedTreeException e) { 
     e.printStackTrace(); 
    } catch (BadLocationException e) { 
     e.printStackTrace(); 
    } 
} 

apply() विधि चाल यहाँ है।

+0

यह मदद करता है, लेकिन यह सुंदर ढंग से doens't सभी का समाधान वांछित स्वरूपण का। अर्थात। आपको कई पैरामीटर के साथ लंबी विधि घोषणाओं को मैन्युअल रूप से तोड़ने की आवश्यकता होगी। –

+0

बहुत ही रोचक समस्या। मैं देखता हूं कि आप क्या कर रहे हैं, और मैंने अभी अपना जवाब संपादित किया है। धन्यवाद। – Ryan

+0

हाय रयान, मैंने उपरोक्त कोड को 'CodeFormatter.K_UNKNOWN' का उपयोग करने के लिए बदल दिया, लेकिन यह भी काम नहीं करता था। बाद में मैंने 'targetUnit.applyTextEdit'' पर कॉल के बाद 'targetUnit.getSource()' की जांच की (जो 'ICompilationUnit' के आंतरिक' IDocument' पर 'लागू() 'है, और अजीब रूप से परिवर्तन लागू होने लगते हैं। लेकिन वे फ़ाइल पर लागू नहीं हैं। क्या यह बग है या मैं कुछ भूल गया हूं? –

0

मैं निम्न विधि का उपयोग फ़ॉर्मेट करने के लिए एक जावा स्रोत फ़ाइल

public static void formatSource(ICompilationUnit cu, IProgressMonitor progressMonitor) throws JavaModelException{ 
    String source = cu.getSource(); 
    IJavaProject javaProject = cu.getJavaProject(); 

    Map options = javaProject.getOptions(true); 


      // Instantiate the default code formatter with the given options 
      final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options); 


      final TextEdit edit = codeFormatter.format(
       CodeFormatter.K_COMPILATION_UNIT, // format a compilation unit 
       source, // source to format 
       0, // starting position 
       source.length(), // length 
       0, // initial indentation 
       System.getProperty("line.separator") // line separator 
      );   

      cu.becomeWorkingCopy(progressMonitor); 
      try { 
       cu.applyTextEdit(edit, progressMonitor); 
       //cu.reconcile(); 
       cu.commitWorkingCopy(true, progressMonitor); 
       //cu.save(progressMonitor, false); 

       JavaUI.openInEditor(cu); 


      } catch (MalformedTreeException e) { 
       e.printStackTrace(); 
      } catch (PartInitException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

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