Sử dụng file template trong Intellij để tạo nhanh file theo mẫu
Configure: Settings | Editor | File and Code Templates
1. File templates
File templates là các mẫu nội dung mặc định khi tạo tệp mới trong IntelliJ IDEA. Chúng cung cấp mã nguồn và định dạng ban đầu theo tiêu chuẩn của từng loại tệp. Các loại tệp được đề xuất tùy thuộc vào module, cấu hình, và vị trí của bạn trong project. Bạn có thể chọn từ danh sách các tệp có sẵn với template tương ứng trong cài đặt.
1.1. Tạo mới template
- Mở cài đặt, sau đó chọn Editor | File and Code Templates.
- Chọn phạm vi áp dụng template qua danh sách Scheme:
- Default: Áp dụng cho tất cả các project trong IDE hiện tại, lưu tại thư mục cấu hình của IDE.
- Project: Áp dụng riêng cho từng project và được lưu trong thư mục .idea/fileTemplates.
- Tại tab Files, nhấn Create Template để tạo template mới, đặt tên, định dạng file và nội dung cho template. Sau đó áp dụng thay đổi và đóng cửa sổ cài đặt.
1.2 Có thể copy từ 1 file template có sẵn
1.3 Để lưu một tệp dưới dạng template, mở tệp trong trình chỉnh sửa.
Trên menu chính, chọn File | Save File as Template.
Trong hộp thoại Save File as Template, đặt tên mới, định dạng và chỉnh sửa nội dung nếu cần. Bạn có thể định sẵn tên tệp hoặc sử dụng các biến để tự động tạo tên. Ví dụ:
File name: Nếu cần, bạn có thể chỉ định tên cho tệp được tạo từ template. Mặc định, IntelliJ IDEA sẽ yêu cầu người dùng nhập tên khi tạo tệp. Bạn có thể cố định một tên cụ thể để bỏ qua bước này, hoặc sử dụng các biến có sẵn để tạo tên tự động. Ví dụ, bạn có thể đặt tệp một thư mục trên bằng cách sử dụng ../${NAME}.
Reformat according to style: Nội dung tạo ra từ template sẽ được định dạng theo quy tắc code style đã thiết lập cho loại tệp đó.
Enable Live Templates: Chèn các live templates vào trong template tệp. Sử dụng cú pháp escape của Velocity để bao gồm các biến live template trong file template, ví dụ:
#[[ $MY_VARIABLE$ $END$ ]]#.
Chọn Reformat according to style để định dạng theo quy tắc code style của loại tệp đó.
Bật Enable Live Templates để chèn live templates vào template tệp. Sau đó, áp dụng thay đổi và đóng hộp thoại.
- Truy cập trang velocity để tham khảo cú pháp
Màn hình thêm mới template
2. Syntax
- Văn bản thuần được giữ nguyên.
- Các biến được thay thế bằng giá trị tương ứng, ví dụ:
${NAME}chèn tên do người dùng cung cấp khi tạo tệp. - Các chỉ thị khác nhau như
#parse,#set,#if, và các chỉ thị khác.
3. Ví dụ
3.1 Tạo mới file template để tạo 1 jekyll new post
- Mong muốn: 1 file sẽ có dạng:
YYYY-MM-DD-(file-name).mdvà Nội dung file sẽ có dạng:
1
2
3
4
5
6
7
8
9
10
11
12
13
---
title: "Sử dụng file template trong Intellij để tạo nhanh file theo mẫu"
description: "mô tả"
author: huy8895
date: 2024-09-07 09:15:00 +0700
categories: [ Tool, Intellij ]
tags: [ Tips, Intellij, 'File Template' ]
pin: false
math: true
mermaid: true
image:
path: assets/img/posts/20240907/0.png
---
- Cách thực hiện:
- Tạị màn hình File and Code Templates chọn thêm mới template +
- name:
Jekyll posttên của file template - Extensions:
mdextension của file muốn tạo. - File name:
1
${YEAR}-${MONTH}-${DAY}-${NAME}.md
- Nội dung file template:
1
2
3
4
5
6
7
8
9
10
11
12
13
---
title: ""
description: ""
author: ${USER}
date: ${YEAR}-${MONTH}-${DAY} ${HOUR}:${MINUTE}:00 +0700
categories: []
tags: []
pin: false
math: true
mermaid: true
image:
path: assets/img/posts/20240907/0.png
---
3.2 Tạo template file log toàn bộ api trong service springboot
- Tạị màn hình File and Code Templates chọn thêm mới template +
- name:
API loggertên của file template - Extensions:
javaextension của file muốn tạo. - Nội dung file template:
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
@Configuration
public class ApiLogger {
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;
@Bean
public CommandLineRunner logApiEndpoints() {
return args -> {
File file = new File("D:\\3.api\\api_endpoints_${PROJECT_NAME}.csv");
// Create parent directories if they do not exist
file.getParentFile().mkdirs();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write("HTTP Method,URL Pattern,Class,Method");
writer.newLine();
Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
handlerMethods.forEach((requestMappingInfo, handlerMethod) -> {
StringBuilder requestMethods = new StringBuilder();
for (RequestMethod requestMethod : requestMappingInfo.getMethodsCondition()
.getMethods()) {
requestMethods.append(requestMethod).append(" ");
}
final String httpMethod = requestMethods.toString().trim();
final String urlPattern = String.valueOf(
requestMappingInfo.getPathPatternsCondition() == null
? requestMappingInfo.getPatternsCondition()
: requestMappingInfo.getPathPatternsCondition());
final String className = (String) handlerMethod.getBean();
final String methodName = handlerMethod.getMethod().getName();
System.out.printf("API: %s %s -> %s%n",
httpMethod,
urlPattern,
className + "." + methodName);
try {
writer.write(String.format("%s,%s,%s,%s",
httpMethod,
urlPattern,
className,
methodName));
writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
});
}
};
}
}
Trên đây là cách mà mình sử dụng file template trong Intellij để tăng hiệu quả cho công việc, rất mong giúp ích được cho các bạn.
Tham khảo thêm tại: File Template
