File: codesnippet.h

package info (click to toggle)
texstudio 2.11.2%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 41,292 kB
  • ctags: 12,405
  • sloc: cpp: 93,072; xml: 10,217; ansic: 4,153; sh: 145; makefile: 56
file content (90 lines) | stat: -rw-r--r-- 4,305 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef CODESNIPPET_H
#define CODESNIPPET_H

#include "mostQtHeaders.h"

class QDocumentCursor;
class QEditor;
class Texstudio;

enum CompletionType {CT_COMMANDS, CT_NORMALTEXT, CT_CITATIONS, CT_CITATIONCOMMANDS, CT_LABELS};

/*!
 * \brief store information for placeholder
 */
struct CodeSnippetPlaceHolder {
	int offset /*< column position*/, length /*< lenght*/;
	int id; ///< identification number to match mirror placeholders
	enum Flag {AutoSelect = 1, Mirrored = 2, Mirror = 4, PreferredMultilineAutoSelect = 8, Persistent = 16, Translatable = 32};
	int flags; ///< type of placeholder
	int offsetEnd() const; ///< return end-column of placeholder = offset+lenght
};
/*!
 * \class CodeSnippet
 * \brief contains data for command completion
 *
 * Handles all information for command completion, especially sort-order, placeholder handling including mirror-placeholders and final cursor positioning
 * Usually these information is encoded via special comments, see manual
 */
class CodeSnippet
{
public:
	CodeSnippet(): cursorLine(-1), cursorOffset(-1), anchorOffset(-1), usageCount(0), index(0), snippetLength(0), type(none) {} ///< generate empty codesnippet
	CodeSnippet(const CodeSnippet &cw): word(cw.word), sortWord(cw.sortWord), lines(cw.lines), cursorLine(cw.cursorLine), cursorOffset(cw.cursorOffset), anchorOffset(cw.anchorOffset), placeHolders(cw.placeHolders), usageCount(cw.usageCount), index(cw.index), snippetLength(cw.snippetLength), type(cw.type), name(cw.name) {} ///< copy constructor
	CodeSnippet(const QString &newWord, bool replacePercentNewline = true); ///< generate codesnippet from text string
	bool operator< (const CodeSnippet &cw) const; ///< define sorting operator
	bool operator== (const CodeSnippet &cw) const;

	enum PlaceholderMode { PlacehodersActive, PlaceholdersToPlainText, PlaceholdersRemoved }; ///< configuaration of placeholder handling

	QString word; ///< displayed snippet name
    QString sortWord; ///< for sorting used snippet name, allows change of sort-order by replacement special characters especially braces
	QStringList lines; ///< to be inserted code
	//TODO: Multirow selection
	int cursorLine;  ///< placement of cursor after insert concerning line of codesnippet; -1 => not defined
	int cursorOffset; ///< placement of cursor after insert concerning column; -1 => not defined
	int anchorOffset; ///< placement of cursor anchor after insert
	QList<QList<CodeSnippetPlaceHolder> > placeHolders; //used to draw
	int usageCount; ///< usage count for favourite detection
	uint index; ///< hash index for usage count identification
	int snippetLength; ///< number of characters
	enum Type {none, length};
	Type type;

	QString expandCode(const QString &code);
	QString environmentContent(const QString &envName);
	void insert(QEditor *editor) const; ///< insert snippet text into given editor at current cursor position
	void insertAt(QEditor *editor, QDocumentCursor *cursor, PlaceholderMode placeholderMode = PlacehodersActive, bool byCompleter = false, bool isKeyVal = false) const;

	void setName(const QString &newName); ///< set codesnippet name, used for usertags
	QString getName() const; ///< get codesnippet name, used for usertags

	static bool autoReplaceCommands;

	static bool debugDisableAutoTranslate;

private:
	QString name;
	QDocumentCursor getCursor(QEditor *editor, const CodeSnippetPlaceHolder &ph, int snippetLine, int baseLine, int baseLineIndent, int lastLineRemainingLength) const;
};
Q_DECLARE_METATYPE(CodeSnippet)
Q_DECLARE_METATYPE(CodeSnippet::PlaceholderMode)

/*!
 * \brief special list class for codesnippets
 *
 * The method for combining are optmized for sorted lists
 */
class CodeSnippetList: public QList<CodeSnippet>
{
public:
	void unite(CodeSnippetList &lst); ///< merge two list (both need to be sorted !)
	void unite(const QList<CodeSnippet> &lst); ///< merge two list (both need to be sorted !)
	void insert(const QString &elem); ///< insert one element into list at correct position to maintain sorting
	CodeSnippetList::iterator insert(CodeSnippetList::iterator before, const CodeSnippet &cs) ///< insert before given iterator
	{
		return QList<CodeSnippet>::insert(before, cs);
	}
};

#endif // CODESNIPPET_H