When should researchers who take part in the composition and editing of a manuscript be co-authors I sometimes work as a ghostwriter/editor for other researchers alongside my own research, and it occurred to me that there isn't a clear framework for crediting this work. I have sometimes appeared in the acknowledgements for writing the first draft or completely rewriting the manuscript, or been a co-author on manuscripts I only commented on or did a tiny bit of data analysis for.

On the one hand, if the editor is employed to edit the language of the manuscript only, I don't think that they should be co-authors. But should they appear in the acknowledgements? Does this depend on whether they were paid specifically for the project or are a departmental editor?

On the other hand, if the editor contributes scientifically to the manuscript, perhaps they should be co-authors. However, does this depend on whether the manuscript is basic research or (for instance) an opinion paper? In this case, how do they differ from a reviewer who comments on the substantive aspects of the paper?
This depends on the field of course, but in general it is pretty unlikely unless the "editor" has contributed to the content of the work, rather than just the wording, layout, etc. 

This has been debated within the Patterns Writing Community within Computer Science. Part of the process of getting a paper accepted goes beyond editing and review. A Shepherd is assigned to each paper and the shepherd works with the authors over three or four iterations/versions of the paper to get it ready for a patterns conference. These conferences are also rather different from what most think of as conferences as they are also designed to improve the paper for publication using Writers Workshops.

Some authors seem to get so much help from their shepherds that they have asked to have the shepherd included as an author. I believe that it has happened in a few cases, but is not a general rule. The shepherd goes far beyond what an editor would do, suggesting content, for example. But even there, it hasn't become a general practice. I'm currently shepherding one paper for an upcoming conference and have made a lot of suggestions, but wouldn't expect to be a co-author, for example. I would, personally, object if a shepherd asked or demanded to be made a co-author, but that is because of the nature of the culture of the patterns community. 

So, if, as you say, the editor contributes content (fact, opinion, whatever), or makes the "author" rethink things generally, then it may be appropriate. But they should be acknowledged in any case.