-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic Go.Mod syntax support (#430)
* feat: basic Go.Mod syntax support * chore: update changelog
- Loading branch information
Showing
8 changed files
with
211 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
web/src/components/features/workspace/CodeEditor/grammar/gomod.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import type { languages } from 'monaco-editor' | ||
|
||
export const conf: languages.LanguageConfiguration = { | ||
comments: { | ||
lineComment: '//', | ||
}, | ||
brackets: [['(', ')']], | ||
autoClosingPairs: [ | ||
{ open: '(', close: ')' }, | ||
{ open: '"', close: '"', notIn: ['string'] }, | ||
{ open: '`', close: '`', notIn: ['string'] }, | ||
], | ||
surroundingPairs: [ | ||
{ open: '(', close: ')' }, | ||
{ open: '"', close: '"' }, | ||
{ open: '`', close: '`' }, | ||
], | ||
} | ||
|
||
export const language: languages.IMonarchLanguage = { | ||
defaultToken: '', | ||
tokenPostfix: '.go.mod', | ||
|
||
keywords: ['module', 'require', 'replace', 'exclude', 'go', 'toolchain'], | ||
operators: ['=>'], | ||
|
||
tokenizer: { | ||
root: [ | ||
// Comments | ||
[/\/\/.*$/, 'comment'], | ||
|
||
// Multi-Line Directive | ||
[ | ||
/^(\w+)(\s*)(\()/, | ||
[ | ||
{ cases: { '@keywords': 'keyword', '@default': 'identifier' }, next: '@directiveArgs' }, | ||
'white', | ||
'delimiter.parenthesis', | ||
], | ||
], | ||
|
||
// Single-Line Directive | ||
[ | ||
/^(\w+)(\s*)(.*)$/, | ||
[{ cases: { '@keywords': 'keyword', '@default': 'identifier' } }, 'white', { token: '', next: '@arguments' }], | ||
], | ||
|
||
// Invalid | ||
[/.*$/, 'invalid'], | ||
], | ||
|
||
directiveArgs: [{ include: '@arguments' }, [/\)/, { token: 'delimiter.parenthesis', next: '@pop' }]], | ||
|
||
arguments: [ | ||
// Comments | ||
[/\/\/.*$/, 'comment'], | ||
|
||
// Double Quoted String | ||
[/"([^"\\]|\\.)*$/, 'string.invalid'], // Non-terminated string | ||
[/"/, { token: 'string.quote', next: '@doubleString' }], | ||
|
||
// Raw Quoted String | ||
[/`/, { token: 'string.quote', next: '@rawString' }], | ||
// Operator | ||
[/=>/, 'operator'], | ||
// Semver Version | ||
[ | ||
/v(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*)?(?:\+[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*)?/, | ||
'number', | ||
], | ||
// Unquoted String | ||
[/([^\s/]|\/(?!\/))+/, 'string'], | ||
// Whitespace | ||
[/\s+/, 'white'], | ||
// End of Line | ||
[/$/, '', '@pop'], | ||
], | ||
doubleString: [ | ||
// Escaped Characters | ||
{ include: '@stringEscapedChar' }, | ||
// Placeholders | ||
{ include: '@stringPlaceholder' }, | ||
// Regular String Content | ||
[/[^\\%"']+/, 'string'], | ||
// Escape at End of Line | ||
[/\\$/, 'string.escape'], | ||
// Closing Quote | ||
[/"/, { token: 'string.quote', next: '@pop' }], | ||
// Invalid Escape | ||
[/\\./, 'string.escape.invalid'], | ||
], | ||
rawString: [ | ||
// Placeholders | ||
{ include: '@stringPlaceholder' }, | ||
// Regular String Content | ||
[/[^%`]+/, 'string'], | ||
|
||
// Percent Sign Not Followed by Placeholder | ||
[/%%/, 'string'], | ||
[/%/, 'string'], | ||
|
||
// Closing Backtick | ||
[/`/, { token: 'string.quote', next: '@pop' }], | ||
], | ||
|
||
stringEscapedChar: [ | ||
[/\\([0-7]{3}|[abfnrtv\\"']|x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/, 'string.escape'], | ||
[/\\./, 'invalid'], | ||
], | ||
|
||
stringPlaceholder: [ | ||
[/%(\[\d+])?([+#\-0\x20]{0,2}((\d+|\*)?(\.?(?:\d+|\*|\[\d+]\*?)?\[\d+]?)?))?[vT%tbcdoqxXUeEfFgGsp]/, 'variable'], | ||
], | ||
}, | ||
} |
22 changes: 22 additions & 0 deletions
22
web/src/components/features/workspace/CodeEditor/grammar/ids.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export enum LanguageID { | ||
Go = 'go', | ||
GoMod = 'go.mod', | ||
GoSum = 'go.sum', | ||
} | ||
|
||
const basename = (filepath: string) => { | ||
const slashPos = filepath.lastIndexOf('/') | ||
return slashPos === -1 ? filepath : filepath.slice(slashPos + 1) | ||
} | ||
|
||
export const languageFromFilename = (filepath: string) => { | ||
const fname = basename(filepath) | ||
switch (fname) { | ||
case 'go.mod': | ||
return LanguageID.GoMod | ||
case 'go.sum': | ||
return LanguageID.GoSum | ||
default: | ||
return fname.endsWith('.go') ? LanguageID.Go : 'plaintext' | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
web/src/components/features/workspace/CodeEditor/grammar/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './ids' | ||
export * from './register' |
41 changes: 41 additions & 0 deletions
41
web/src/components/features/workspace/CodeEditor/grammar/register.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import * as monaco from 'monaco-editor' | ||
|
||
import { LanguageID } from './ids' | ||
|
||
const isLangRegistered = (langId: string) => { | ||
// Monaco doesn't provide quick search | ||
const match = monaco.languages.getLanguages().filter(({ id }) => id === langId) | ||
return !!match.length | ||
} | ||
|
||
const concatDisposables = (...items: monaco.IDisposable[]): monaco.IDisposable => ({ | ||
dispose: () => { | ||
items.forEach((i) => i.dispose()) | ||
}, | ||
}) | ||
|
||
export const registerExtraLanguages = (): monaco.IDisposable => { | ||
console.log('register') | ||
if (!isLangRegistered(LanguageID.GoMod)) { | ||
monaco.languages.register({ | ||
id: 'go.mod', | ||
extensions: ['.mod'], | ||
filenames: ['go.mod'], | ||
aliases: ['GoMod'], | ||
}) | ||
} | ||
|
||
return concatDisposables( | ||
monaco.languages.registerTokensProviderFactory(LanguageID.GoMod, { | ||
create: async () => { | ||
const mod = await import('./gomod.ts') | ||
return mod.language | ||
}, | ||
}), | ||
|
||
monaco.languages.onLanguageEncountered(LanguageID.GoMod, async () => { | ||
const mod = await import('./gomod.ts') | ||
monaco.languages.setLanguageConfiguration(LanguageID.GoMod, mod.conf) | ||
}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters