I wrote a little about this a while ago:
https://sketchplugins.com/d/1430-where-is-the-documentation-for-context-api/2
I wanna know is there a document that explains how could we use the headers document correctly?
No, there is no documentation because its the internal Sketch API. The recommended approach is to use the JS API and if there are things you wish to be added you can make an issue or PR here https://github.com/sketch-hq/SketchAPI
For example, how could I know that I need to use documentData() to access MSDocumentData? How could I find their right corresponding relationship?
I'd probably start by looking at the sketch headers: https://github.com/abynim/Sketch-Headers
If you press "t" on that page you can search for the MSDocumentData file. This will show you the methods for the MSDocumentData class. Unfortunately, you will have to do some more digging to understand how this class is used.
You can do a search within the repo for MSDocumentData. https://github.com/abynim/Sketch-Headers/search?p=1&q=MSDocumentData
and you will find on page 3 that MSDocument has a method called documentData: https://github.com/abynim/Sketch-Headers/blob/0a6393be39975eb6a871ad5f390b197ea538325d/Headers/MSDocument.h#L46
So what does this mean for your plugin? Well it means you can do something like this:
context.document.documentData()
Note that I'm using context
here to get at the MSDocument
object. If I wanted to use the sketch API to get at the same info it would look like this:
let sketch = require('sketch')
let doc = sketch.getSelectedDocument()
let docInternal = doc.sketchObject
let documentData = docInternal.documentData()
As always, the internals can AND DO CHANGE from release to release so if your plugin relies on these internal methods then you will need to make sure you are adding the relevant try/catch statements to make sure that your code doesn't crash (and then update your plugin with the new methods). This is why the recommended approach is to add an issue or PR for the JS API so that the sketch team can abstract some of these problems away for you.
Last but not least, reading the headers will make much more sense if you learn more about objective-c app development on the Mac. If you are interested in learning more on this somewhat lost art then I recommend watching these videos: https://www.youtube.com/watch?v=X_MJd8wqTBM&list=PLE83F832121568D36
There is a lot there (and some stuff has changed since those videos were made) but if you stick with it, this stuff will make muchhhh more sense.
Hope that helps!