Skip to content

Commit a37ea15

Browse files
committed
Display sample code for each language in themes
1 parent 005185e commit a37ea15

File tree

8 files changed

+239
-50
lines changed

8 files changed

+239
-50
lines changed

app/src/main/java/com/duy/ccppcompiler/ide/editor/theme/ThemeActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public Fragment getItem(int position) {
6565
return null;
6666
}
6767

68-
@Nullable
6968
@Override
7069
public CharSequence getPageTitle(int position) {
7170
switch (position) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Block comment
3+
*/
4+
#include <vector>
5+
6+
using namespace std; // line comment
7+
namespace foo {
8+
9+
typedef struct Struct {
10+
int field;
11+
} Typedef;
12+
enum Enum {Foo = 1, Bar = 2};
13+
14+
Typedef *globalVar;
15+
extern Typedef *externVar;
16+
17+
template<typename T, int N>
18+
class Class {
19+
T n;
20+
public:
21+
/**
22+
* Semantic highlighting:
23+
* Generated spectrum to pick colors for local variables and parameters:
24+
* Color#1 SC1.1 SC1.2 SC1.3 SC1.4 Color#2 SC2.1 SC2.2 SC2.3 SC2.4 Color#3
25+
* Color#3 SC3.1 SC3.2 SC3.3 SC3.4 Color#4 SC4.1 SC4.2 SC4.3 SC4.4 Color#5
26+
*/
27+
void function(int param1, int param2, int param3) {
28+
int localVar1, localVar2, localVar3;
29+
int *localVar = new int[1];
30+
this->n = N;
31+
localVar1 = param1 + param2 + localVar3;
32+
33+
label:
34+
printf("Formatted string %d\n\g", localVar[0]);
35+
printf(R"**(Formatted raw-string %d\n)**", 1);
36+
std::cout << (1 << 2) << std::endl;
37+
38+
#define FOO(A) A
39+
#ifdef DEBUG
40+
printf("debug");
41+
#endif
42+
}
43+
};
44+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Block comment */
2+
import java.util.Date;
3+
/**
4+
* Doc comment here for <code>SomeClass</code>
5+
* @param T type parameter
6+
* @see Math#sin(double)
7+
*/
8+
@Annotation (name=value)
9+
public class SomeClass<T extends Runnable> { // some comment
10+
private T field = null;
11+
private double unusedField = 12345.67890;
12+
private UnknownType anotherString = "Another\nStrin\g";
13+
public static int staticField = 0;
14+
public final int instanceFinalField = 0;
15+
16+
/**
17+
* Semantic highlighting:
18+
* Generated spectrum to pick colors for local variables and parameters:
19+
* Color#1 SC1.1 SC1.2 SC1.3 SC1.4 Color#2 SC2.1 SC2.2 SC2.3 SC2.4 Color#3
20+
* Color#3 SC3.1 SC3.2 SC3.3 SC3.4 Color#4 SC4.1 SC4.2 SC4.3 SC4.4 Color#5
21+
* @param param1
22+
* @param reassignedParam
23+
* @param param2
24+
* @param param3
25+
*/
26+
public SomeClass(AnInterface param1, int[] reassignedParam,
27+
int param2
28+
int param3) {
29+
int reassignedValue = this.staticField + param2 + param3;
30+
long localVar1, localVar2, localVar3, localVar4;
31+
int localVar = "IntelliJ"; // Error, incompatible types
32+
System.out.println(anotherString + toString() + localVar);
33+
long time = Date.parse("1.2.3"); // Method is deprecated
34+
new Thread().countStackFrames(); // Method is deprecated and marked for removal
35+
reassignedValue ++;
36+
field.run();
37+
new SomeClass() {
38+
{
39+
int a = localVar;
40+
}
41+
};
42+
reassignedParam = new ArrayList<String>().toArray(new int[0]);
43+
}
44+
}
45+
enum AnEnum { CONST1, CONST2 }
46+
interface AnInterface {
47+
int CONSTANT = 2;
48+
void method();
49+
}
50+
abstract class SomeAbstractClass {
51+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{ File header }
2+
program sample;
3+
var
4+
i, j: Integer;
5+
s: string;
6+
a: array[0..5] of string;
7+
const
8+
c = 'a';
9+
begin
10+
@s[1] := 'str';
11+
i := j mod 200 + j xor j^ + Round(0.7 + .1);
12+
for i := 0 to length(s)-1 do begin
13+
writeln('Hello world!');
14+
end;
15+
@error?;
16+
end.

lib-n-ide/src/main/java/com/duy/ide/editor/theme/EditorThemeFragment.java

Lines changed: 78 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.annotation.SuppressLint;
44
import android.content.Context;
5+
import android.content.res.AssetManager;
56
import android.os.AsyncTask;
67
import android.os.Bundle;
78
import android.support.annotation.NonNull;
@@ -19,18 +20,20 @@
1920

2021
import com.duy.ide.editor.Highlighter;
2122
import com.duy.ide.editor.editor.R;
23+
import com.duy.ide.editor.theme.model.Constants;
2224
import com.duy.ide.editor.theme.model.EditorTheme;
2325
import com.duy.ide.editor.view.IEditAreaView;
2426
import com.jecelyin.editor.v2.Preferences;
2527
import com.jecelyin.editor.v2.highlight.Buffer;
2628
import com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView;
2729

30+
import org.apache.commons.io.IOUtils;
2831
import org.gjt.sp.jedit.Catalog;
2932
import org.gjt.sp.jedit.Mode;
3033

34+
import java.io.InputStream;
3135
import java.util.ArrayList;
32-
import java.util.Collections;
33-
import java.util.Comparator;
36+
import java.util.Arrays;
3437
import java.util.HashMap;
3538

3639
public class EditorThemeFragment extends Fragment {
@@ -54,8 +57,12 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
5457
mRecyclerView = view.findViewById(R.id.recyclerView);
5558
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
5659
mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
57-
loadData();
5860

61+
mEditorThemeAdapter = new EditorThemeAdapter(getContext());
62+
mEditorThemeAdapter.setOnThemeSelectListener((EditorThemeAdapter.OnThemeSelectListener) getActivity());
63+
mRecyclerView.setAdapter(mEditorThemeAdapter);
64+
65+
loadData();
5966
}
6067

6168
private void loadData() {
@@ -83,17 +90,36 @@ public static class EditorThemeAdapter extends RecyclerView.Adapter<EditorThemeA
8390
private final ArrayList<EditorTheme> mEditorThemes;
8491
private Context mContext;
8592
private OnThemeSelectListener onThemeSelectListener;
86-
private Mode mLanguage = Catalog.getModeByName("C++");
93+
private Mode mLanguage;
94+
private String mSampleCode;
8795

8896
EditorThemeAdapter(Context context) {
8997
mContext = context;
90-
mEditorThemes = ThemeLoader.getAll(context);
91-
Collections.sort(mEditorThemes, new Comparator<EditorTheme>() {
92-
@Override
93-
public int compare(EditorTheme o1, EditorTheme o2) {
94-
return o1.getName().compareTo(o2.getName());
98+
mEditorThemes = new ArrayList<>();
99+
resolveLanguage();
100+
}
101+
102+
private void resolveLanguage() {
103+
if (mSampleCode == null) {
104+
try {
105+
String fileName = null;
106+
if (mContext.getPackageName().contains("cpp")) {
107+
fileName = "templates/cplusplus.template";
108+
mLanguage = Catalog.getModeByName("C++");
109+
} else if (mContext.getPackageName().contains("java")) {
110+
fileName = "templates/java.template";
111+
mLanguage = Catalog.getModeByName("Java");
112+
} else if (mContext.getPackageName().contains("pascal")) {
113+
fileName = "templates/pascal.template";
114+
mLanguage = Catalog.getModeByName("Pascal");
115+
}
116+
InputStream input = mContext.getAssets().open(fileName);
117+
mSampleCode = IOUtils.toString(input);
118+
input.close();
119+
} catch (Exception e) {
120+
e.printStackTrace();
95121
}
96-
});
122+
}
97123
}
98124

99125
int getPosition(EditorTheme editorTheme) {
@@ -150,39 +176,13 @@ public int getItemCount() {
150176
return mEditorThemes.size();
151177
}
152178

179+
153180
private String getSampleData() {
154-
return "// C++ Program to Find Largest Element of an Array\n" +
155-
"\n" +
156-
"// This program takes n number of element from user (where, n is specified by user) and stores data in an array. Then, this program displays the largest element of that array using loops.\n" +
157-
"\n" +
158-
"#include <iostream>\n" +
159-
"\n" +
160-
"using namespace std;\n" +
161-
"\n" +
162-
"int main() {\n" +
163-
" int i, n;\n" +
164-
" float arr[100];\n" +
165-
"\n" +
166-
" cout << \"Enter total number of elements(1 to 100): \";\n" +
167-
" cin >> n;\n" +
168-
" cout << endl;\n" +
169-
"\n" +
170-
" // Store number entered by the user\n" +
171-
" for (i = 0; i < n; ++i) {\n" +
172-
" cout << \"Enter Number \" << i + 1 << \" : \";\n" +
173-
" cin >> arr[i];\n" +
174-
" }\n" +
175-
"\n" +
176-
" // Loop to store largest number to arr[0]\n" +
177-
" for (i = 1; i < n; ++i) {\n" +
178-
" // Change < to > if you want to find the smallest element\n" +
179-
" if (arr[0] < arr[i])\n" +
180-
" arr[0] = arr[i];\n" +
181-
" }\n" +
182-
" cout << \"Largest element = \" << arr[0];\n" +
183-
"\n" +
184-
" return 0;\n" +
185-
"}";
181+
182+
if (mSampleCode == null) {
183+
mSampleCode = Constants.C_PLUS_PLUS_SAMPLE;
184+
}
185+
return mSampleCode;
186186
}
187187

188188
@NonNull
@@ -195,6 +195,11 @@ public void setOnThemeSelectListener(OnThemeSelectListener onThemeSelectListener
195195
this.onThemeSelectListener = onThemeSelectListener;
196196
}
197197

198+
public void addTheme(EditorTheme theme) {
199+
mEditorThemes.add(theme);
200+
notifyItemInserted(mEditorThemes.size() - 1);
201+
}
202+
198203
public interface OnThemeSelectListener {
199204
void onEditorThemeSelected(EditorTheme theme);
200205
}
@@ -204,7 +209,7 @@ static class ViewHolder extends RecyclerView.ViewHolder {
204209
IEditAreaView mEditorView;
205210
TextView mTxtName;
206211

207-
public ViewHolder(View itemView) {
212+
ViewHolder(View itemView) {
208213
super(itemView);
209214
mEditorView = itemView.findViewById(R.id.editor_view);
210215
mTxtName = itemView.findViewById(R.id.txt_name);
@@ -213,8 +218,11 @@ public ViewHolder(View itemView) {
213218
}
214219
}
215220

216-
private class LoadThemeTask extends AsyncTask<Void, Void, Void> {
221+
@SuppressLint("StaticFieldLeak")
222+
private class LoadThemeTask extends AsyncTask<Void, EditorTheme, Void> {
223+
private static final String TAG = "LoadThemeTask";
217224
private Context context;
225+
private AssetManager mAssetManager;
218226

219227
LoadThemeTask(Context context) {
220228
this.context = context;
@@ -227,27 +235,49 @@ public Context getContext() {
227235
@Override
228236
protected void onPreExecute() {
229237
super.onPreExecute();
238+
mAssetManager = getContext().getAssets();
239+
230240
mProgressBar.setVisibility(View.VISIBLE);
231241
mProgressBar.setIndeterminate(true);
232242
}
233243

234244
@Override
235245
protected Void doInBackground(Void... voids) {
236-
ThemeLoader.init(getContext());
246+
try {
247+
String[] list = mAssetManager.list(ThemeLoader.ASSET_PATH);
248+
Arrays.sort(list);
249+
for (String name : list) {
250+
if (isCancelled()) {
251+
return null;
252+
}
253+
Thread.sleep(1);
254+
EditorTheme theme = ThemeLoader.getTheme(context, name);
255+
publishProgress(theme);
256+
}
257+
} catch (Exception e) {
258+
e.printStackTrace();
259+
}
237260
return null;
238261
}
239262

263+
@Override
264+
protected void onProgressUpdate(EditorTheme... themes) {
265+
super.onProgressUpdate(themes);
266+
try {
267+
mEditorThemeAdapter.addTheme(themes[0]);
268+
} catch (Exception e) {
269+
}
270+
}
271+
240272
@Override
241273
protected void onPostExecute(Void aVoid) {
242274
super.onPostExecute(aVoid);
243275
if (isCancelled()) {
244276
return;
245277
}
246-
mEditorThemeAdapter = new EditorThemeAdapter(getContext());
247-
mEditorThemeAdapter.setOnThemeSelectListener((EditorThemeAdapter.OnThemeSelectListener) getActivity());
248-
mRecyclerView.setAdapter(mEditorThemeAdapter);
249278
mRecyclerView.scrollToPosition(findThemeIndex(mPreferences.getEditorTheme()));
250279
mProgressBar.setVisibility(View.GONE);
280+
251281
}
252282
}
253283
}

lib-n-ide/src/main/java/com/duy/ide/editor/theme/ThemeLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import java.util.Properties;
1717

1818
public class ThemeLoader {
19-
private static final String ASSET_PATH = "themes/vscode";
19+
public static final String ASSET_PATH = "themes/vscode";
2020
private static final String DEFAULT_EDITOR_THEME_LIGHT = "github-light.json.properties";
2121
private static final HashMap<String, EditorTheme> CACHED = new HashMap<>();
2222
private static final String TAG = "ThemeLoader";

0 commit comments

Comments
 (0)