Skip to content

Commit 63eb20b

Browse files
author
sakuraash
committed
fix language mode, add makefile
1 parent 0a6f66f commit 63eb20b

File tree

4 files changed

+106
-61
lines changed

4 files changed

+106
-61
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"id": "makefile",
3+
"aliases": ["makefile", "Makefile"],
4+
"extensions": [
5+
".make",
6+
".GNUmakefile",
7+
".makefile",
8+
".Makefile",
9+
".OCamlMakefile",
10+
".mak",
11+
".mk"
12+
]
13+
}

app/components/MonacoEditor/MonacoReact/grammars/languages.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ export default [
138138
mimeType: ['application/javascript'],
139139
aliases: ['JavaScript']
140140
},
141+
{
142+
id: 'makefile',
143+
aliases: ['makefile', 'Makefile'],
144+
extensions: ['.make', '.GNUmakefile', '.makefile', '.Makefile', '.OCamlMakefile', '.mak', '.mk'],
145+
filenames: ['Makefile', 'OCamlMakefile'],
146+
scopeName: 'source.makefile'
147+
},
141148
{
142149
id: 'json',
143150
scopeName: 'source.json',

app/components/MonacoEditor/state.js

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,32 @@ import uniqueId from 'lodash/uniqueId'
22
import React from 'react'
33
import { render } from 'react-dom'
44
import { observe, observable, computed, action, extendObservable, reaction } from 'mobx'
5-
65
import mime from 'mime-types'
76
import { Services } from 'monaco-languageclient'
87

9-
import codeEditorService from './codeEditorService'
108
import assignProps from 'utils/assignProps'
119
import getTabType from 'utils/getTabType'
1210
import is from 'utils/is'
11+
import config from 'config'
1312
import TabStore from 'components/Tab/store'
1413
import FileStore from 'commons/File/store'
1514
import EditorState from 'components/Editor/state'
1615
import { createMonacoServices } from 'components/MonacoEditor/Editors/createHelper'
17-
import { findLanguageByextensions, findModeByName } from './utils/findLanguage'
16+
import {
17+
findLanguageByextensions,
18+
findModeByName,
19+
findLanguageFileName
20+
} from './utils/findLanguage'
21+
import codeEditorService from './codeEditorService'
1822
import ConditionWidget from './ConditionWidget'
1923
import initialOptions from './monacoDefaultOptions'
20-
import config from 'config'
2124

22-
reaction(() => initialOptions.theme, (theme) => {
23-
monaco.editor.setTheme(theme)
24-
})
25+
reaction(
26+
() => initialOptions.theme,
27+
(theme) => {
28+
monaco.editor.setTheme(theme)
29+
}
30+
)
2531

2632
const state = observable({
2733
entities: observable.map({}),
@@ -30,7 +36,7 @@ const state = observable({
3036
activeMonacoEditor: null,
3137
editors: new Map(),
3238
activeEditorListeners: [],
33-
installed: false,
39+
installed: false
3440
})
3541

3642
const typeDetect = (title, types) => {
@@ -46,10 +52,9 @@ class EditorInfo {
4652
EditorState.entities.set(this.id, this)
4753
this.update(props)
4854
this.uri = this.filePath
49-
? (this.filePath.startsWith('jdt://')
50-
|| this.filePath.startsWith('omnisharp-metadata://')
51-
? this.filePath
52-
: `file://${config._ROOT_URI_}${this.filePath}`)
55+
? this.filePath.startsWith('jdt://') || this.filePath.startsWith('omnisharp-metadata://')
56+
? this.filePath
57+
: `file://${config._ROOT_URI_}${this.filePath}`
5358
: `inmemory://model/${this.id}`
5459

5560
if (!props.filePath || this.isMonaco) {
@@ -64,11 +69,14 @@ class EditorInfo {
6469
this.monacoElement.style.height = '100%'
6570

6671
if (this.filePath) {
67-
this.languageMode = findLanguageByextensions(this.filePath.split('.').pop()).id
72+
const fileName = this.filePath.split('/').pop()
73+
this.languageMode = fileName.includes('.')
74+
? findLanguageByextensions(fileName.split('.').pop()).id
75+
: findLanguageFileName(fileName).id
6876
}
6977
const model =
70-
monaco.editor.getModel(monaco.Uri.parse(this.uri).toString()) ||
71-
monaco.editor.createModel(this.content || '', this.languageMode, monaco.Uri.parse(this.uri))
78+
monaco.editor.getModel(monaco.Uri.parse(this.uri).toString()) ||
79+
monaco.editor.createModel(this.content || '', this.languageMode, monaco.Uri.parse(this.uri))
7280
this.uri = model.uri._formatted
7381

7482
this.model = model
@@ -87,7 +95,9 @@ class EditorInfo {
8795

8896
if (!state.installed) {
8997
// install Monaco language client services
90-
const services = createMonacoServices(monacoEditor, { rootUri: `file://${config._ROOT_URI_}` })
98+
const services = createMonacoServices(monacoEditor, {
99+
rootUri: `file://${config._ROOT_URI_}`
100+
})
91101
Services.install(services)
92102
state.installed = true
93103
}
@@ -108,13 +118,15 @@ class EditorInfo {
108118
}
109119
}
110120

111-
this.disposers.push(observe(this, 'content', (change) => {
112-
const content = change.newValue || ''
113-
if (content !== monacoEditor.getValue()) {
114-
this.startsWithUTF8BOM = this.content.charCodeAt(0) === 65279
115-
monacoEditor.setValue(content)
116-
}
117-
}))
121+
this.disposers.push(
122+
observe(this, 'content', (change) => {
123+
const content = change.newValue || ''
124+
if (content !== monacoEditor.getValue()) {
125+
this.startsWithUTF8BOM = this.content.charCodeAt(0) === 65279
126+
monacoEditor.setValue(content)
127+
}
128+
})
129+
)
118130

119131
if (this.content && this.content.length > 0) {
120132
this.startsWithUTF8BOM = this.content.charCodeAt(0) === 65279
@@ -137,10 +149,10 @@ class EditorInfo {
137149
monacoEditor.addAction({
138150
id: 'custom-comment',
139151
label: 'comment',
140-
keybindings: [
141-
monaco.KeyMod.CtrlCmd | monaco.KeyCode.US_SLASH
142-
],
143-
run: () => { /* no */ }
152+
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.US_SLASH],
153+
run: () => {
154+
/* no */
155+
}
144156
})
145157

146158
monacoEditor.onDidChangeCursorPosition((event) => {
@@ -157,7 +169,7 @@ class EditorInfo {
157169
if (props.selection) {
158170
const pos = {
159171
lineNumber: props.selection.startLineNumber,
160-
column: props.selection.startColumn,
172+
column: props.selection.startColumn
161173
}
162174
setTimeout(() => {
163175
monacoEditor.setSelection(props.selection)
@@ -186,7 +198,7 @@ class EditorInfo {
186198
const [lineNumber, column] = position.split(':')
187199
const pos = {
188200
lineNumber: Number(lineNumber),
189-
column: Number(column),
201+
column: Number(column)
190202
}
191203
this.monacoEditor.setPosition(pos)
192204
this.monacoEditor.revealPositionInCenter(pos, 1)
@@ -233,45 +245,43 @@ class EditorInfo {
233245
}
234246
}
235247

236-
setViewZoneForBreakPoint = (breakpoint) => {
237-
return new Promise((resolve, reject) => {
238-
const { path, line } = breakpoint
239-
240-
this.monacoEditor.changeViewZones((changeAccessor) => {
241-
const domNode = document.createElement('div')
242-
const viewZoneId = changeAccessor.addZone({
243-
afterLineNumber: line,
244-
heightInLines: 2,
245-
afterColumn: 0,
246-
domNode,
247-
})
248-
const handleCancel = () => {
249-
this.monacoEditor.changeViewZones((_changeAccessor) => {
250-
_changeAccessor.removeZone(viewZoneId)
251-
})
252-
}
253-
render(<ConditionWidget onChange={resolve} onCancel={handleCancel} breakpoint={breakpoint} />, domNode)
248+
setViewZoneForBreakPoint = breakpoint => new Promise((resolve, reject) => {
249+
const { path, line } = breakpoint
250+
251+
this.monacoEditor.changeViewZones((changeAccessor) => {
252+
const domNode = document.createElement('div')
253+
const viewZoneId = changeAccessor.addZone({
254+
afterLineNumber: line,
255+
heightInLines: 2,
256+
afterColumn: 0,
257+
domNode
254258
})
259+
const handleCancel = () => {
260+
this.monacoEditor.changeViewZones((_changeAccessor) => {
261+
_changeAccessor.removeZone(viewZoneId)
262+
})
263+
}
264+
render(
265+
<ConditionWidget onChange={resolve} onCancel={handleCancel} breakpoint={breakpoint} />,
266+
domNode
267+
)
255268
})
256-
}
269+
})
257270

258271
setDebuggerBreakPoint = (params) => {
259272
const { line, verified } = params
260273
const debuggerBreakPoint = this.debugBreakPoints.get(line)
261-
const newBreakPoint = this.monacoEditor.deltaDecorations(
262-
debuggerBreakPoint || [],
263-
[
264-
{
265-
range: new monaco.Range(line, 1, line, 1),
266-
options: {
267-
isWholeLine: false,
268-
glyphMarginClassName: verified
269-
? 'monaco-glyphMargin-breakpoint'
270-
: 'monaco-glyphMargin-breakpoint-unverified'
271-
}
274+
const newBreakPoint = this.monacoEditor.deltaDecorations(debuggerBreakPoint || [], [
275+
{
276+
range: new monaco.Range(line, 1, line, 1),
277+
options: {
278+
isWholeLine: false,
279+
glyphMarginClassName: verified
280+
? 'monaco-glyphMargin-breakpoint'
281+
: 'monaco-glyphMargin-breakpoint-unverified'
272282
}
273-
]
274-
)
283+
}
284+
])
275285
this.debugBreakPoints.set(line, newBreakPoint)
276286
}
277287

app/components/MonacoEditor/utils/findLanguage.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ function findLanguageByextensions (ext) {
1818
}
1919
return { id: 'plaintext' }
2020
}
21+
22+
function findLanguageFileName (fileName) {
23+
const monacoLanguages = monaco.languages.getLanguages()
24+
for (let i = 0; i < monacoLanguages.length; i++) {
25+
const info = monacoLanguages[i]
26+
if (info.filenames) {
27+
for (let j = 0; j < info.filenames.length; j++) {
28+
if (info.filenames[j] === fileName) return info
29+
}
30+
}
31+
}
32+
return { id: 'plaintext' }
33+
}
34+
2135
/**
2236
* 根据文件列表粗略判断当前项目语言,以启动相应的语言服务器
2337
* @param data 指定目录下文件列表
@@ -73,4 +87,5 @@ export {
7387
findLanguageByextensions,
7488
findModeByName,
7589
findLanguagesByFileList,
90+
findLanguageFileName,
7691
}

0 commit comments

Comments
 (0)