Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit 7c0cd69

Browse files
author
Maximilian Schmidt
authored
Merge pull request #10 from MCStreetguy/pending-major
Snippet handling changed to UID
2 parents 926adb6 + 3314ac9 commit 7c0cd69

File tree

7 files changed

+383
-98
lines changed

7 files changed

+383
-98
lines changed

CHANGELOG.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@
33

44
----------------
55

6+
## v1.0.0
7+
**Stable release**
8+
#### Features
9+
- Snippet class:
10+
- Title
11+
- Tags *(not used currently)*
12+
- Content
13+
- Language
14+
- UID
15+
- Version
16+
- Automated snippet migration to newer version
17+
#### Improvements
18+
- Performance
19+
- `LICENSE.md` / `README.md`
20+
- Icon styling
21+
- Delete prompt with list
22+
#### Patches
23+
- UID generation
24+
- Storage fixes for new snippet conventions
25+
- Version comparison
26+
- Delete prompt styling
27+
28+
---
29+
630
## v0.1.0 ~ v1.0.0
731
**Early Development**
832
#### Features
@@ -30,3 +54,5 @@
3054
- JSON error on snippet saving
3155
- Error with different path dividers on different OSs
3256
- Icon update when searching prompt list
57+
58+
---

lib/icon-helper.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/snippet-injector.js

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ export default {
1313

1414
activate(state) {
1515
this.storage = new Storage({dir: '/storage/snippet-injector/snippets/'});
16-
console.log(this.storage);
16+
this.storage.migrate();
1717

18-
// Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
1918
this.subscriptions = new CompositeDisposable();
20-
// Register command that toggles this view
2119
this.subscriptions.add(atom.commands.add('atom-workspace', {
2220
'snippet-injector:create': () => this.create(),
2321
'snippet-injector:insert': () => this.insert(),
@@ -39,17 +37,26 @@ export default {
3937
var selection = atom.workspace.getActiveTextEditor().getSelectedText();
4038
var grammar = atom.workspace.getActiveTextEditor().getGrammar().name;
4139
var storage = this.storage;
40+
var uid = Util.generateUID({
41+
unique: true,
42+
tester: storage.testFile,
43+
timeout: 100,
44+
length: 20,
45+
prefix: 'sn',
46+
insertstring: 'SNIPPET'
47+
});
4248
const inputPrompt = Util.promptUser({
4349
placeholder: 'Enter snippet title',
4450
btnconfirm: 'Save snippet',
4551
btncancel: 'Cancel'
4652
},function(text){
47-
if(text !== null) {
53+
if(Util.isset(text,'string')) {
4854
var result = storage.store(new Snippet({
4955
title: text,
5056
tags: new Array(),
5157
content: selection,
52-
lang: grammar
58+
lang: grammar,
59+
uid: uid
5360
}));
5461
if(result) {
5562
atom.notifications.addSuccess('Snippet "'+text+'" was saved successfully.', null);
@@ -64,13 +71,11 @@ export default {
6471
var storage = this.storage;
6572
var filenames = storage.retrieveFiles();
6673
if(filenames.length > 0) {
67-
var snippets = new Array();
6874
var listitems = new Array();
6975
var icons = new Array();
7076
filenames.forEach(function(currentValue) {
71-
var elem = new Snippet(JSON.parse(storage.retrieveFile(currentValue.replace('.snippet.json',''))));
72-
snippets.push(elem);
73-
listitems.push(elem.getTitle());
77+
var elem = new Snippet(JSON.parse(storage.retrieveFile(currentValue.replace('.json',''))));
78+
listitems.push(elem.getUID());
7479
icons.push(elem.getLang());
7580
});
7681

@@ -82,7 +87,7 @@ export default {
8287
nothingfound: 'No Snippets found that match you search.',
8388
icons: icons
8489
},function(element) {
85-
if(element !== null) {
90+
if(Util.isset(element,'string')) {
8691
var snippet = new Snippet(JSON.parse(storage.retrieveFile(element)));
8792
atom.workspace.getActiveTextEditor().insertText(snippet.getContent(),{
8893
select: true,
@@ -91,7 +96,7 @@ export default {
9196
autoDecreaseIndent: true,
9297
normalizeLineEndings: true
9398
});
94-
atom.notifications.addInfo('Snippet \''+element+'\' was successfully inserted.', null);
99+
atom.notifications.addSuccess('Snippet \''+snippet.getTitle()+'\' was successfully inserted.', null);
95100
}
96101
});
97102
} else {
@@ -101,20 +106,35 @@ export default {
101106

102107
delete() {
103108
var storage = this.storage;
104-
const deletePrompt = Util.promptDelete({
105-
title: 'Delete a snippet',
106-
placeholder: 'Title of the snippet that shall be deleted',
107-
btnconfirm: 'Delete',
108-
btncancel: 'Cancel',
109-
suremsg: 'Sure?'
110-
},function(element) {
111-
if(element !== null) {
112-
if(storage.deleteFile(element)) {
113-
atom.notifications.addSuccess('Snippet \''+element+'\' was successfully deleted.', null);
114-
} else {
115-
atom.notifications.addWarning('Snippet \''+element+'\' could not be found. Is it spelled right?', null);
109+
var fileNames = storage.retrieveFiles();
110+
if(fileNames.length > 0) {
111+
var items = new Array();
112+
fileNames.forEach(function(current) {
113+
var snippet = new Snippet(JSON.parse(storage.retrieveFile(current.replace('.json',''))));
114+
items.push({
115+
value: snippet.getUID(),
116+
title: snippet.getTitle()
117+
});
118+
});
119+
const deletePrompt = Util.promptDelete({
120+
title: 'Delete a snippet',
121+
placeholder: 'Please choose a snippet...',
122+
btnconfirm: 'Delete',
123+
btncancel: 'Cancel',
124+
suremsg: 'Are you sure?',
125+
items: items
126+
},function(element) {
127+
if(Util.isset(element,'string')) {
128+
var snippet = new Snippet(JSON.parse(storage.retrieveFile(element)));
129+
if(storage.deleteFile(snippet.getUID())) {
130+
atom.notifications.addSuccess('Snippet \''+snippet.getTitle()+'\' was successfully deleted.', null);
131+
} else {
132+
atom.notifications.addWarning('Snippet \''+snippet.getTitle()+'\' could not be found in the local storage.', null);
133+
}
116134
}
117-
}
118-
});
135+
});
136+
} else {
137+
atom.notifications.addWarning('No snippets found in the local storage directory.', null);
138+
}
119139
}
120140
};

lib/snippet.js

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
'use babel';
22

3+
import Util from './util.js';
4+
35
export default class Snippet {
46

57
content = '';
68
title = '';
79
tags = new Array();
810
lang = '';
11+
uid = Date.now().toString();
12+
version = undefined;
913

1014
constructor(state) {
11-
if(state !== undefined && state !== null && typeof state === 'object') {
12-
this.title = state.title;
13-
this.tags = state.tags;
14-
this.content = state.content;
15-
this.lang = state.lang;
16-
} else {
17-
this.title = "";
18-
this.tags = new Array();
19-
this.content = "";
20-
tis.lang = "";
15+
if(typeof state === 'object') {
16+
if(Util.isset(state.title,'string')) {
17+
this.title = state.title;
18+
}
19+
if(Util.isset(state.tags,'object')) {
20+
this.tags = state.tags;
21+
}
22+
if(Util.isset(state.content,'string')) {
23+
this.content = state.content;
24+
}
25+
if(Util.isset(state.lang,'string')) {
26+
this.lang = state.lang;
27+
}
28+
if(Util.isset(state.uid,'string')) {
29+
this.uid = state.uid;
30+
}
31+
if(Util.isset(state.version,'string')) {
32+
this.version = state.version;
33+
}
2134
}
2235
}
2336

2437
// Returns an object that can be retrieved when package is activated
2538
serialize() {
2639
return {
27-
'title':this.title,
28-
'tags':this.tags,
29-
'content':this.content,
30-
'lang':this.lang
40+
'title':this.getTitle(),
41+
'tags':this.getTags(),
42+
'content':this.getContent(),
43+
'lang':this.getLang(),
44+
'uid':this.getUID(),
45+
'version':this.getVersion()
3146
};
3247
}
3348

@@ -50,4 +65,36 @@ export default class Snippet {
5065
getLang() {
5166
return this.lang;
5267
}
68+
69+
getUID() {
70+
return this.uid;
71+
}
72+
73+
getVersion() {
74+
return this.version;
75+
}
76+
77+
setTitle(newtitle) {
78+
this.title = newtitle;
79+
}
80+
81+
setContent(newcontent) {
82+
this.content = newcontent;
83+
}
84+
85+
setTags(newtags) {
86+
this.tags = newtags;
87+
}
88+
89+
setLang(newlang) {
90+
this.lang = newlang;
91+
}
92+
93+
setVersion(newversion) {
94+
this.version = newversion;
95+
}
96+
97+
setUID(newuid) {
98+
this.uid = newuid;
99+
}
53100
}

0 commit comments

Comments
 (0)