Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdb9b3a6

Browse files
committed
feat(lint): add lint inspection for sketch patches
This commit introduces a new lint inspection feature for sketch patches. It includes the `SketchCodeInspection` and `SketchInspectionError` classes to handle and display lint errors. Additionally, the `SingleFileDiffView` has been updated to perform lint checks on the new code after applying a patch and display any errors found.
1 parent71a3918 commitdb9b3a6

File tree

4 files changed

+195
-10
lines changed

4 files changed

+195
-10
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
packagecom.phodal.shirecore.sketch.lint
2+
3+
importcom.intellij.analysis.AnalysisScope
4+
importcom.intellij.codeInsight.daemon.impl.DaemonProgressIndicator
5+
importcom.intellij.codeInspection.InspectionEngine
6+
importcom.intellij.codeInspection.InspectionManager
7+
importcom.intellij.codeInspection.ProblemDescriptor
8+
importcom.intellij.codeInspection.ex.GlobalInspectionContextBase
9+
importcom.intellij.codeInspection.ex.LocalInspectionToolWrapper
10+
importcom.intellij.lang.annotation.HighlightSeverity
11+
importcom.intellij.openapi.application.runReadAction
12+
importcom.intellij.openapi.project.Project
13+
importcom.intellij.openapi.ui.popup.JBPopup
14+
importcom.intellij.openapi.ui.popup.JBPopupFactory
15+
importcom.intellij.openapi.vfs.VirtualFile
16+
importcom.intellij.profile.codeInspection.InspectionProjectProfileManager
17+
importcom.intellij.psi.PsiFile
18+
importcom.intellij.psi.PsiManager
19+
importcom.intellij.ui.JBColor
20+
importcom.intellij.ui.components.JBLabel
21+
importcom.intellij.ui.components.JBScrollPane
22+
importcom.intellij.ui.table.JBTable
23+
importcom.intellij.util.PairProcessor
24+
importcom.phodal.shirecore.ShireCoreBundle
25+
importjava.awt.Dimension
26+
importjava.awt.FlowLayout
27+
importjava.awt.event.MouseAdapter
28+
importjava.awt.event.MouseEvent
29+
importjavax.swing.BorderFactory
30+
importjavax.swing.JPanel
31+
importjavax.swing.JTable
32+
importjavax.swing.table.DefaultTableModel
33+
34+
object SketchCodeInspection {
35+
funshowErrors(errors:List<SketchInspectionError>,panel:JPanel) {
36+
val columnNames= arrayOf("Line","Description","Highlight Type")
37+
val data= errors.map {
38+
arrayOf(it.lineNumber, it.description, it.highlightType.toString())
39+
}.toTypedArray()
40+
41+
val tableModel=DefaultTableModel(data, columnNames)
42+
43+
val table=JBTable(tableModel).apply {
44+
autoResizeMode=JTable.AUTO_RESIZE_ALL_COLUMNS
45+
}
46+
47+
val scrollPane=JBScrollPane(table).apply {
48+
preferredSize=Dimension(480,400)
49+
}
50+
51+
val errorLabel=JBLabel(ShireCoreBundle.message("sketch.lint.error", errors.size)).apply {
52+
border=BorderFactory.createEmptyBorder(5,5,5,5)
53+
addMouseListener(object:MouseAdapter() {
54+
overridefunmouseClicked(e:MouseEvent?) {
55+
createPopup(scrollPane, table, errors).showInCenterOf(panel)
56+
}
57+
58+
overridefunmouseEntered(e:MouseEvent) {
59+
toolTipText=ShireCoreBundle.message("sketch.lint.error.tooltip")
60+
}
61+
})
62+
}
63+
64+
val errorPanel=JPanel().apply {
65+
background=JBColor.WHITE
66+
layout=FlowLayout(FlowLayout.LEFT)
67+
add(errorLabel)
68+
}
69+
70+
panel.add(errorPanel)
71+
}
72+
73+
privatefuncreatePopup(
74+
scrollPane:JBScrollPane,
75+
table:JBTable,
76+
errors:List<SketchInspectionError>
77+
):JBPopup=JBPopupFactory.getInstance()
78+
.createComponentPopupBuilder(scrollPane, table)
79+
.setTitle("Found Lint Issues:${errors.size}")
80+
.setResizable(true)
81+
.setMovable(true)
82+
.setRequestFocus(true)
83+
.createPopup()
84+
85+
funrunInspections(project:Project,psiFile:PsiFile,originFile:VirtualFile):List<SketchInspectionError> {
86+
val globalContext=InspectionManager.getInstance(project).createNewGlobalContext()
87+
as?GlobalInspectionContextBase?:return emptyList()
88+
89+
val originPsi= runReadAction {PsiManager.getInstance(project).findFile(originFile) }
90+
?:return emptyList()
91+
92+
globalContext.currentScope=AnalysisScope(originPsi)
93+
94+
val toolsCopy= collectTools(project, psiFile, globalContext)
95+
if (toolsCopy.isEmpty()) {
96+
return emptyList()
97+
}
98+
99+
return runReadAction {
100+
val indicator=DaemonProgressIndicator()
101+
val result:Map<LocalInspectionToolWrapper,List<ProblemDescriptor>>=InspectionEngine.inspectEx(
102+
toolsCopy, psiFile, psiFile.textRange, psiFile.textRange,false,false,true,
103+
indicator,PairProcessor.alwaysTrue<LocalInspectionToolWrapper?,ProblemDescriptor?>()
104+
)
105+
106+
val problems= result.values.flatten()
107+
return@runReadAction problems
108+
.sortedBy { it.lineNumber }
109+
.distinctBy { it.lineNumber }.map {
110+
SketchInspectionError.Companion.from(it)
111+
}
112+
}
113+
}
114+
115+
privatefuncollectTools(
116+
project:Project,
117+
psiFile:PsiFile,
118+
globalContext:GlobalInspectionContextBase
119+
):MutableList<LocalInspectionToolWrapper> {
120+
val inspectionProfile=InspectionProjectProfileManager.getInstance(project).currentProfile
121+
val toolWrappers= inspectionProfile.getInspectionTools(psiFile)
122+
.filter {
123+
it.isApplicable(psiFile.language)&& it.defaultLevel.severity==HighlightSeverity.ERROR
124+
}
125+
126+
toolWrappers.forEach {
127+
it.initialize(globalContext)
128+
}
129+
130+
val toolsCopy:MutableList<LocalInspectionToolWrapper>=
131+
ArrayList<LocalInspectionToolWrapper>(toolWrappers.size)
132+
for (toolin toolWrappers) {
133+
if (toolisLocalInspectionToolWrapper) {
134+
toolsCopy.add(tool.createCopy())
135+
}
136+
}
137+
138+
return toolsCopy
139+
}
140+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
packagecom.phodal.shirecore.sketch.lint
2+
3+
importcom.intellij.codeInspection.ProblemDescriptor
4+
importcom.intellij.codeInspection.ProblemHighlightType
5+
6+
data classSketchInspectionError(
7+
vallineNumber:Int,
8+
valdescription:String,
9+
valhighlightType:ProblemHighlightType,
10+
) {
11+
companionobject {
12+
funfrom(problemDescriptor:ProblemDescriptor):SketchInspectionError {
13+
returnSketchInspectionError(
14+
problemDescriptor.lineNumber,
15+
problemDescriptor.descriptionTemplate,
16+
problemDescriptor.highlightType
17+
)
18+
}
19+
}
20+
}

‎core/src/main/kotlin/com/phodal/shirecore/sketch/patch/SingleFileDiffView.kt‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import com.intellij.diff.editor.DiffEditorTabFilesManager
88
importcom.intellij.diff.requests.SimpleDiffRequest
99
importcom.intellij.icons.AllIcons
1010
importcom.intellij.lang.Language
11+
importcom.intellij.openapi.application.ApplicationManager
1112
importcom.intellij.openapi.application.runInEdt
13+
importcom.intellij.openapi.application.runReadAction
1214
importcom.intellij.openapi.command.undo.UndoManager
1315
importcom.intellij.openapi.diff.impl.patch.TextFilePatch
1416
importcom.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier
@@ -18,15 +20,19 @@ import com.intellij.openapi.project.Project
1820
importcom.intellij.openapi.ui.DialogPanel
1921
importcom.intellij.openapi.vfs.VirtualFile
2022
importcom.intellij.openapi.vfs.readText
23+
importcom.intellij.psi.PsiManager
24+
importcom.intellij.testFramework.LightVirtualFile
2125
importcom.intellij.ui.DarculaColors
2226
importcom.intellij.ui.JBColor
2327
importcom.intellij.ui.components.JBLabel
2428
importcom.intellij.ui.components.panels.VerticalLayout
2529
importcom.intellij.ui.dsl.builder.AlignX
2630
importcom.intellij.ui.dsl.builder.RightGap
2731
importcom.intellij.ui.dsl.builder.panel
32+
importcom.intellij.util.LocalTimeCounter
2833
importcom.phodal.shirecore.ShireCoreBundle
2934
importcom.phodal.shirecore.sketch.LangSketch
35+
importcom.phodal.shirecore.sketch.lint.SketchCodeInspection
3036
importjava.awt.BorderLayout
3137
importjava.awt.event.MouseAdapter
3238
importjava.awt.event.MouseEvent
@@ -38,27 +44,30 @@ import javax.swing.JPanel
3844

3945
classSingleFileDiffView(
4046
privatevalmyProject:Project,
41-
privatevalvirtualFile:VirtualFile,
47+
privatevalcurrentFile:VirtualFile,
4248
valpatch:TextFilePatch,
4349
) : LangSketch {
4450
privateval mainPanel:JPanel=JPanel(VerticalLayout(5))
4551
privateval myHeaderPanel:JPanel=JPanel(BorderLayout())
4652
privatevar filePanel:DialogPanel?=null
4753
var diffFile:ChainDiffVirtualFile?=null
54+
privateval oldCode= currentFile.readText()
55+
privateval appliedPatch=GenericPatchApplier.apply(oldCode, patch.hunks)
56+
privateval newCode= appliedPatch?.patchedText?:""
4857

4958
init {
5059
val contentPanel=JPanel(BorderLayout())
5160
val actions= createActionButtons()
52-
val filepathLabel=JBLabel(virtualFile.name).apply {
53-
icon=virtualFile.fileType.icon
61+
val filepathLabel=JBLabel(currentFile.name).apply {
62+
icon=currentFile.fileType.icon
5463
border=BorderFactory.createEmptyBorder(2,10,2,10)
5564

5665
addMouseListener(object:MouseAdapter() {
5766
overridefunmouseClicked(e:MouseEvent?) {
5867
val isShowDiffSuccess= showDiff()
5968
if (isShowDiffSuccess)return
6069

61-
FileEditorManager.getInstance(myProject).openFile(virtualFile,true)
70+
FileEditorManager.getInstance(myProject).openFile(currentFile,true)
6271
}
6372

6473
overridefunmouseEntered(e:MouseEvent) {
@@ -94,6 +103,20 @@ class SingleFileDiffView(
94103

95104
mainPanel.add(myHeaderPanel)
96105
mainPanel.add(contentPanel)
106+
107+
ApplicationManager.getApplication().executeOnPooledThread {
108+
lintCheckForNewCode(currentFile)
109+
}
110+
}
111+
112+
funlintCheckForNewCode(currentFile:VirtualFile) {
113+
if (newCode.isEmpty())return
114+
val newFile=LightVirtualFile(currentFile, newCode,LocalTimeCounter.currentTime())
115+
val psiFile= runReadAction {PsiManager.getInstance(myProject).findFile(newFile) }?:return
116+
val errors=SketchCodeInspection.runInspections(myProject, psiFile, currentFile)
117+
if (errors.isNotEmpty()) {
118+
SketchCodeInspection.showErrors(errors,this@SingleFileDiffView.mainPanel)
119+
}
97120
}
98121

99122
privatefunshowDiff():Boolean {
@@ -102,13 +125,13 @@ class SingleFileDiffView(
102125
returntrue
103126
}
104127

105-
val document=FileDocumentManager.getInstance().getDocument(virtualFile)?:returnfalse
128+
val document=FileDocumentManager.getInstance().getDocument(currentFile)?:returnfalse
106129
val appliedPatch=GenericPatchApplier.apply(document.text, patch.hunks)
107130
?:returnfalse
108131

109132
val newText= appliedPatch.patchedText
110133
val diffFactory=DiffContentFactoryEx.getInstanceEx()
111-
val currentDocContent= diffFactory.create(myProject,virtualFile)
134+
val currentDocContent= diffFactory.create(myProject,currentFile)
112135
val newDocContent= diffFactory.create(newText)
113136

114137
val diffRequest=
@@ -120,7 +143,7 @@ class SingleFileDiffView(
120143
"AI generated"
121144
)
122145

123-
val producer=SimpleDiffRequestProducer.create(virtualFile.path) {
146+
val producer=SimpleDiffRequestProducer.create(currentFile.path) {
124147
diffRequest
125148
}
126149

@@ -141,7 +164,7 @@ class SingleFileDiffView(
141164

142165
privatefuncreateActionButtons():List<JButton> {
143166
val undoManager=UndoManager.getInstance(myProject)
144-
val fileEditor=FileEditorManager.getInstance(myProject).getSelectedEditor(virtualFile)
167+
val fileEditor=FileEditorManager.getInstance(myProject).getSelectedEditor(currentFile)
145168

146169
val rollback=JButton(AllIcons.Actions.Rollback).apply {
147170
toolTipText=ShireCoreBundle.message("sketch.patch.action.rollback.tooltip")
@@ -162,7 +185,7 @@ class SingleFileDiffView(
162185
returnlistOf(rollback)
163186
}
164187

165-
overridefungetViewText():String=virtualFile.readText()
188+
overridefungetViewText():String=currentFile.readText()
166189

167190
overridefunupdateViewText(text:String) {}
168191

‎core/src/main/resources/messages/ShireCoreBundle.properties‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ sketch.patch.action.viewDiff.tooltip=View the diff
2323
sketch.patch.action.rollback=Rollback
2424
sketch.patch.action.rollback.tooltip=Rollback the change
2525
chat.panel.send=Send
26-
chat.input.empty.tips=Input cannot be empty
26+
chat.input.empty.tips=Input cannot be empty
27+
sketch.lint.error.tooltip=Click to view all errors
28+
sketch.lint.error=Found Lint issue: {0}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp