15
15
#include < coreplugin/messagemanager.h>
16
16
#include < texteditor/texteditor.h>
17
17
#include < texteditor/textdocument.h>
18
+ #include < projectexplorer/project.h>
19
+ #include < projectexplorer/projectexplorer.h>
20
+ #include < projectexplorer/projectnodes.h>
21
+ #include < projectexplorer/projecttree.h>
18
22
19
23
#include < QtPlugin>
20
24
#include < QAction>
25
29
26
30
using namespace Core ;
27
31
using namespace TextEditor ;
32
+ using namespace ProjectExplorer ;
28
33
29
34
namespace GitHubGist {
30
35
namespace Internal {
@@ -61,8 +66,8 @@ bool GistPlugin::initialize(const QStringList &arguments, QString *errorString)
61
66
connect (m_gistManager, &GistManager::apiError, this , &GistPlugin::showMessage);
62
67
connect (m_gistManager, &GistManager::gistPosted, this , &GistPlugin::gistCreated);
63
68
64
- createMenu ();
65
- createOptionsPage ();
69
+ initMenus ();
70
+ initOptionsPage ();
66
71
67
72
return true ;
68
73
}
@@ -133,7 +138,7 @@ static inline void fixSpecialCharacters(QString &data)
133
138
}
134
139
}
135
140
136
- void GistPlugin::createGist ()
141
+ void GistPlugin::createGistFromText ()
137
142
{
138
143
QString text;
139
144
QString fileName;
@@ -150,50 +155,123 @@ void GistPlugin::createGist()
150
155
m_gistManager->postGist (text, fileName, fileName, publicFlag);
151
156
}
152
157
153
- void GistPlugin::createMenu ()
158
+ void GistPlugin::createGistFromNode ()
154
159
{
160
+ const Node* node = ProjectTree::currentNode ();
161
+ if (!node) {
162
+ return ;
163
+ }
164
+
165
+ QStringList files = nodeFiles (node);
166
+ if (!files.isEmpty ()) {
167
+ m_gistManager->postGist (files, ProjectTree::currentProject ()->displayName ());
168
+ }
169
+ }
170
+
171
+ static inline void addToMenu (Command *cmd, Id id) {
172
+ ActionContainer *container = ActionManager::actionContainer (id);
173
+ if (container) {
174
+ container->addAction (cmd);
175
+ }
176
+ }
177
+
178
+ void GistPlugin::initMenus ()
179
+ {
180
+ // ! Tools->GitHubGist menu actions
155
181
QAction *createPublicAction = new QAction (QIcon (QLatin1String (" :/images/gist.png" )),
156
182
tr (" Create gist" ), this );
157
183
createPublicAction->setProperty (" public" , true );
184
+ connect (createPublicAction, &QAction::triggered, this , &GistPlugin::createGistFromText);
158
185
Command *createPublicGistCmd = ActionManager::registerAction (createPublicAction,
159
186
Constants::CREATE_PUBLIC_ACTION_ID,
160
187
Context (Core::Constants::C_EDIT_MODE));
161
188
createPublicGistCmd->setDefaultKeySequence (QKeySequence (tr (" Ctrl+Alt+G" )));
162
- connect (createPublicAction, &QAction::triggered, this , &GistPlugin::createGist);
163
189
164
190
QAction *createSecretAction = new QAction (QIcon (QLatin1String (" :/images/gist-secret.png" )),
165
191
tr (" Create secret gist" ), this );
166
192
createSecretAction->setProperty (" public" , false );
193
+ connect (createSecretAction, &QAction::triggered, this , &GistPlugin::createGistFromText);
167
194
Command *createSecretGistCmd = ActionManager::registerAction (createSecretAction,
168
195
Constants::CREATE_SECRET_ACTION_ID,
169
196
Context (Core::Constants::C_EDIT_MODE));
170
- connect (createSecretAction, &QAction::triggered, this , &GistPlugin::createGist);
171
197
172
198
ActionContainer *gistsMenu = ActionManager::createMenu (Constants::GIST_TOOLS_MENU_ID);
173
199
gistsMenu->menu ()->setTitle (tr (" GitHub Gist" ));
174
200
gistsMenu->addAction (createPublicGistCmd);
175
201
gistsMenu->addAction (createSecretGistCmd);
176
202
ActionManager::actionContainer (Core::Constants::M_TOOLS)->addMenu (gistsMenu);
203
+
204
+ // ! ProjectExplorer context menu actions
205
+ QAction *gistFromNode = new QAction (tr (" Create Gist" ), this );
206
+ connect (gistFromNode, &QAction::triggered, this , &GistPlugin::createGistFromNode);
207
+
208
+ Command *gistFromNodeCmd = ActionManager::registerAction (gistFromNode,
209
+ Constants::CREATE_GIST_FROM_NODE,
210
+ Context (Core::Constants::C_NAVIGATION_PANE));
211
+
212
+ addToMenu (gistFromNodeCmd, ProjectExplorer::Constants::M_FILECONTEXT);
213
+ addToMenu (gistFromNodeCmd, ProjectExplorer::Constants::M_FOLDERCONTEXT);
214
+ addToMenu (gistFromNodeCmd, ProjectExplorer::Constants::M_PROJECTCONTEXT);
215
+ addToMenu (gistFromNodeCmd, ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
177
216
}
178
217
179
- void GistPlugin::createOptionsPage ()
218
+ void GistPlugin::initOptionsPage ()
180
219
{
181
220
m_optionsPage = new OptionsPage (m_settings, this );
182
221
addAutoReleasedObject (m_optionsPage);
183
222
}
184
223
185
- void GistPlugin::showMessage (const QString &message)
224
+ void GistPlugin::showMessage (const QString &message) const
186
225
{
187
226
MessageManager::write (message);
188
227
}
189
228
190
- void GistPlugin::gistCreated (const QString &name, const QString &url)
229
+ void GistPlugin::gistCreated (const QString &name, const QString &url) const
191
230
{
192
231
if (m_settings->autoCopyLink ) {
193
232
QApplication::clipboard ()->setText (url);
194
233
}
195
234
showMessage (tr (" Gist \" %1\" posted: " ).arg (name) + url);
196
235
}
197
236
237
+ QStringList GistPlugin::nodeFiles (const Node *node) const
238
+ {
239
+ if (!node) {
240
+ return QStringList ();
241
+ }
242
+
243
+ QStringList files;
244
+
245
+ switch (node->nodeType ()) {
246
+ case FileNodeType: {
247
+ const FileNode *file = static_cast <const FileNode *>(node);
248
+ if (file) {
249
+ files << file->path ().toString ();
250
+ }
251
+ break ;
252
+ }
253
+ case ProjectNodeType:
254
+ case FolderNodeType:
255
+ case VirtualFolderNodeType: {
256
+ const FolderNode *folder = static_cast <const FolderNode *>(node);
257
+ if (folder) {
258
+ for (auto subfolder : folder->subFolderNodes ()) {
259
+ files.append (nodeFiles (subfolder));
260
+ }
261
+ for (auto file : folder->fileNodes ()) {
262
+ // ! TODO NOTE: This condition require for create gist from text/plain files only
263
+ if (file->fileType () != FileType::UnknownFileType)
264
+ files.append (nodeFiles (file));
265
+ }
266
+ }
267
+ break ;
268
+ }
269
+ default :
270
+ break ;
271
+ }
272
+
273
+ return files;
274
+ }
275
+
198
276
} // namespace Internal
199
277
} // namespace GitHubGist
0 commit comments